i have a existing table with a primary key over 3 columns (1 varchar and 2 integer). How can i tell the entity framework to use this "key". Is it possible using the modelBuilder, attributes or is there another way ?
thanks!
In fluent api you must use anonymous type:
modelBuilder.Entity<YourType>()
.HasKey(e => new
{
e.VarChar,
e.Int1,
e.Int2
});
Other way is using data annotations:
public class YourType
{
[Key, Column(Order = 0)]
public string VarChar { get; set; }
[Key, Column(Order = 1)]
public int Int1 { get; set; }
[Key, Column(Order = 2)]
public int Int2 { get; set; }
}
In both scenarios order of columns is important. Once you try to use DbSet<YourType>.Find you will have to supply keys in the same order. EF also uses order internally.