Creating string index with Code first

c# entity-framework indexing performance sql-server

Question

The domain model I'm using with Entity Framework 6.1 is shown below.

class Item
{
    [Index]
    public string CreatedBy { set; get; }
} 

I receive the next error when I use update-database for migration. But as far as my research goes[Index] should serve as a complement tostring .

Column 'CreatedBy' in table 'dbo.Items' is of a type that is invalid for use as a key column in an index.

1
37
9/13/2016 6:34:24 PM

Accepted Answer

ZZZ_tmp
91
12/29/2014 11:12:35 AM

Popular Answer

If you utilize mappings, also known as entity type configuration:

public class MyPocoEntitiyTypeConfig<T> : EntityTypeConfiguration<T> where T:class
{

}

public class MyPocoEnt
{
    public virtual string MyProp { get; set; }
}

public class MyPocoEntMapping : MyPocoEntitiyTypeConfig<MyPocoEnt>
{
    public MyPocoEntMapping()
    {
            Property(x => x.MyProp).HasMaxLength(300);
            Property(x => x.MyProp).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("MyProp") { IsUnique = true }));
        }
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow