c# - Use enum as FK in EF6 -
we have enum supplier
but need have domain data on relation
so in 99.9% in domain code doe operations on enum product.supplier == suppliers.fedex
but have added product.supplierinfo.canadjustpickuptime
supplierinfo
entity , not simple enum type.
i have tried these configs
property(p => p.supplier) .isrequired() .hascolumnname("supplierid"); hasrequired(p => p.supplierinfo) .withmany() .hasforeignkey(p => p.supplier); //i have tried casting int doing .hasforeignkey(p => (int)p.supplier)
this fail
the resulttype of specified expression not compatible required type. expression resulttype 'myapp.model.suppliers' required type 'edm.int32'. parameter name: keyvalues[0]
also tried
property(l => l.supplier) .isrequired() .hascolumnname("supplierid"); hasrequired(p => p.supplierinfo) .withmany() .map(m => m.mapkey("supplierid"));
this offcourse give old
one or more validation errors detected during model generation:
supplierid: name: each property name in type must unique. property name 'supplierid' defined.
i offcourse define supplierid property use hasforeignkey
need change .suppliedid == (int)suppliers.fedex
etc. not solution.
i add property enum uses supplierid property backing field, not work expressions since needs use real mapped db properties
any ideas?
i have classes:
public class agreement { public int id { get; set; } public agreementstatetypeenum agreementstateid { get; set; } } public class agreementstate { public int id { get; set; } public string title { get; set; } }
context:
public class agreementcontext :dbcontext { public agreementcontext() : base("sqlconnection") { } public dbset<agreement> agreements { get; set; } }
in method onmodelcreating wrote nothing. enum:
public enum agreementstatetypeenum : int { inreviewing = 1, confirmed = 2, rejected = 3 }
in database: in table agreements have foreign key agreementstateid - link table agreementstates. working. example:
var temp = context.agreements.first(x => x.agreementstateid == agreementstatetypeenum.confirmed);
i use enum how foreign key.
Comments
Post a Comment