Advertisement
Guest User

Unique nullable index

a guest
Jun 23rd, 2014
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. //using NullableUniqueDemo.Migrations;
  2. using System;
  3. using System.Data.Entity;
  4. using System.Data.Entity.Infrastructure;
  5.  
  6. namespace NullableUniqueDemo
  7. {
  8.     public class Program
  9.     {
  10.         public static void Main()
  11.         {
  12.             //Database.SetInitializer<MyContext>(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
  13.  
  14.             MyContext db = new MyContext();
  15.             db.ExampleTable.Add(new MyClass() { UniqueColumn = "One" });
  16.             db.ExampleTable.Add(new MyClass() { UniqueColumn = "Two" });
  17.             db.SaveChanges();
  18.  
  19.             db.ExampleTable.Add(new MyClass() { UniqueColumn = null });
  20.             db.ExampleTable.Add(new MyClass() { UniqueColumn = null });
  21.  
  22.             // There is no exception when adding two entries with null values for the unique column.
  23.             db.SaveChanges();
  24.  
  25.             db.ExampleTable.Add(new MyClass() { UniqueColumn = "One" });
  26.  
  27.             // Error when you add entry with non-unique 'UniqueColumn'
  28.             db.SaveChanges();
  29.         }
  30.     }
  31.  
  32.     public class MyClass
  33.     {
  34.         public int Id { get; set; }
  35.         public string UniqueColumn { get; set; }
  36.     }
  37.  
  38.     public class MyContext : DbContext
  39.     {
  40.         public DbSet<MyClass> ExampleTable { get; set; }
  41.     }
  42. }
  43.  
  44.  
  45. // Migration Folder
  46. namespace NullableUniqueDemo.Migrations
  47. {
  48.     using System;
  49.     using System.Data.Entity.Migrations;
  50.  
  51.     internal sealed class Configuration : DbMigrationsConfiguration<NullableUniqueDemo.MyContext>
  52.     {
  53.         public Configuration()
  54.         {
  55.             AutomaticMigrationsEnabled = false;
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61. namespace NullableUniqueDemo.Migrations
  62. {
  63.     using System;
  64.     using System.Data.Entity.Migrations;
  65.  
  66.     public partial class IndexedMigration : DbMigration
  67.     {
  68.         public override void Up()
  69.         {
  70.             CreateTable(
  71.                 "dbo.MyClasses",
  72.                 c => new
  73.                     {
  74.                         Id = c.Int(nullable: false, identity: true),
  75.                         UniqueColumn = c.String(nullable: true, maxLength: 450),
  76.                     })
  77.                 .PrimaryKey(t => t.Id);
  78.  
  79.             // Add unique nullable index
  80.             string indexName = "IX_UQ_UniqueColumn";
  81.             string tableName = "dbo.MyClasses";
  82.             string columnName = "UniqueColumn";
  83.  
  84.             Sql(string.Format(@"
  85.                CREATE UNIQUE NONCLUSTERED INDEX {0}
  86.                ON {1}({2})
  87.                WHERE {2} IS NOT NULL;",
  88.                 indexName, tableName, columnName));
  89.  
  90.             //CreateIndex(
  91.             //    table: "dbo.ExampleClasses",
  92.             //    columns: new string[] { "UniqueColumn" },
  93.             //    unique: true,
  94.             //    name: "IX_UniqueColumn",
  95.             //    clustered: false,
  96.             //    anonymousArguments: new
  97.             //    {
  98.             //        Include = new string[] { "UniqueColumn" },
  99.             //        Where = "UniqueColumn IS NOT NULL"
  100.             //    });
  101.         }
  102.  
  103.         public override void Down()
  104.         {
  105.             DropIndex("dbo.MyClasses", "IX_UQ_UniqueColumn");
  106.             DropTable("dbo.MyClasses");
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112. // <auto-generated />
  113. namespace ConsoleApplication8.Migrations
  114. {
  115.     using System.CodeDom.Compiler;
  116.     using System.Data.Entity.Migrations;
  117.     using System.Data.Entity.Migrations.Infrastructure;
  118.     using System.Resources;
  119.    
  120.     [GeneratedCode("EntityFramework.Migrations", "6.1.1-30610")]
  121.     public sealed partial class Migration5 : IMigrationMetadata
  122.     {
  123.         private readonly ResourceManager Resources = new ResourceManager(typeof(Migration5));
  124.        
  125.         string IMigrationMetadata.Id
  126.         {
  127.             get { return "201406231015119_Migration5"; }
  128.         }
  129.        
  130.         string IMigrationMetadata.Source
  131.         {
  132.             get { return null; }
  133.         }
  134.        
  135.         string IMigrationMetadata.Target
  136.         {
  137.             get { return Resources.GetString("Target"); }
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement