c# - Entity Framework composite primary key with Object type -
i using entity framework code first, want generate db code below: have 2 entity class, short example:
product { public int id; public int name; } month{ public int year; public int month; }
now want create class 'report'
report{ public product product; public month month; public decimal price; }
i tried add together composite primary key in dbcontext
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<report>().haskey(t => new { t.product.id, t.month.id }); }
but ef tell 'report' must have primary key primitive type (such int, char...) how can prepare this?
you need add together composite key study class. need identifier on month class. syntax below may not 100%.
public class study { [key(order=0)] public int productid { get; set; } [key(order=1)] public int monthid { get; set; } public product product { get; set; } public month month { get; set; } public decimal cost { get; set; } }
c# entity-framework ef-code-first composite-primary-key
No comments:
Post a Comment