Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //using NullableUniqueDemo.Migrations;
- using System;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- namespace NullableUniqueDemo
- {
- public class Program
- {
- public static void Main()
- {
- //Database.SetInitializer<MyContext>(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
- MyContext db = new MyContext();
- db.ExampleTable.Add(new MyClass() { UniqueColumn = "One" });
- db.ExampleTable.Add(new MyClass() { UniqueColumn = "Two" });
- db.SaveChanges();
- db.ExampleTable.Add(new MyClass() { UniqueColumn = null });
- db.ExampleTable.Add(new MyClass() { UniqueColumn = null });
- // There is no exception when adding two entries with null values for the unique column.
- db.SaveChanges();
- db.ExampleTable.Add(new MyClass() { UniqueColumn = "One" });
- // Error when you add entry with non-unique 'UniqueColumn'
- db.SaveChanges();
- }
- }
- public class MyClass
- {
- public int Id { get; set; }
- public string UniqueColumn { get; set; }
- }
- public class MyContext : DbContext
- {
- public DbSet<MyClass> ExampleTable { get; set; }
- }
- }
- // Migration Folder
- namespace NullableUniqueDemo.Migrations
- {
- using System;
- using System.Data.Entity.Migrations;
- internal sealed class Configuration : DbMigrationsConfiguration<NullableUniqueDemo.MyContext>
- {
- public Configuration()
- {
- AutomaticMigrationsEnabled = false;
- }
- }
- }
- namespace NullableUniqueDemo.Migrations
- {
- using System;
- using System.Data.Entity.Migrations;
- public partial class IndexedMigration : DbMigration
- {
- public override void Up()
- {
- CreateTable(
- "dbo.MyClasses",
- c => new
- {
- Id = c.Int(nullable: false, identity: true),
- UniqueColumn = c.String(nullable: true, maxLength: 450),
- })
- .PrimaryKey(t => t.Id);
- // Add unique nullable index
- string indexName = "IX_UQ_UniqueColumn";
- string tableName = "dbo.MyClasses";
- string columnName = "UniqueColumn";
- Sql(string.Format(@"
- CREATE UNIQUE NONCLUSTERED INDEX {0}
- ON {1}({2})
- WHERE {2} IS NOT NULL;",
- indexName, tableName, columnName));
- //CreateIndex(
- // table: "dbo.ExampleClasses",
- // columns: new string[] { "UniqueColumn" },
- // unique: true,
- // name: "IX_UniqueColumn",
- // clustered: false,
- // anonymousArguments: new
- // {
- // Include = new string[] { "UniqueColumn" },
- // Where = "UniqueColumn IS NOT NULL"
- // });
- }
- public override void Down()
- {
- DropIndex("dbo.MyClasses", "IX_UQ_UniqueColumn");
- DropTable("dbo.MyClasses");
- }
- }
- }
- // <auto-generated />
- namespace ConsoleApplication8.Migrations
- {
- using System.CodeDom.Compiler;
- using System.Data.Entity.Migrations;
- using System.Data.Entity.Migrations.Infrastructure;
- using System.Resources;
- [GeneratedCode("EntityFramework.Migrations", "6.1.1-30610")]
- public sealed partial class Migration5 : IMigrationMetadata
- {
- private readonly ResourceManager Resources = new ResourceManager(typeof(Migration5));
- string IMigrationMetadata.Id
- {
- get { return "201406231015119_Migration5"; }
- }
- string IMigrationMetadata.Source
- {
- get { return null; }
- }
- string IMigrationMetadata.Target
- {
- get { return Resources.GetString("Target"); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement