c# - Entity Framework Code First Composite Foreign Key on legacy DB -
so, i've been banging head few days , searching has led me close solution.
i have legacy database thats far following ef's code convention , i'm using ef code first.
this actual situation: (irrelevant fields ommited sake of brevity)
[table("pedido")] public class pedido : ivalidatableobject { [key, column(order = 1), required] [databasegenerated(databasegeneratedoption.identity)] public int codigo { get; set; } [key, column(order = 2), foreignkey("dadoscliente"), required] public int cliente { get; set; } public virtual cliente dadoscliente { get; set; } public virtual icollection<materialpedido> dadosmateriaispedido { get; set; } } [table("material_pedido")] public class materialpedido : ivalidatableobject { [key, required, column(order = 1)] public int nrpedido { get; set; } // column relates pedido.codigo [key, required, column(order = 2), foreignkey("dadoscliente")] public int cliente { get; set; } public virtual cliente dadoscliente { get; set; } [key, required, column(order = 3)] public string codigo { get; set; } // please note column sort of "virtual field". value should hard-coded "p" when relating table "pedido" } public class entitiescontext : dbcontext { protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.entity<pedido>() .haskey(p => new { p.cliente, p.codigo }) .hasmany(x => x.dadosmateriaispedido) .withoptional(p => p.pedido) .hasforeignkey(x => new { x.cliente, x.nrpedido }) .willcascadeondelete(false); } }
as of i'm getting following error:
projetopi.ef.pedido_dadosmateriaispedido: : multiplicity conflicts referential constraint in role 'pedido_dadosmateriaispedido_source' in relationship 'pedido_dadosmateriaispedido'. because of properties in dependent role non-nullable, multiplicity of principal role must '1'. pedido_dadosmateriaispedido_target: : multiplicity not valid in role 'pedido_dadosmateriaispedido_target' in relationship 'pedido_dadosmateriaispedido'. because dependent role refers key properties, upper bound of multiplicity of dependent role must '1'.
what missing?any appreciated!
so, i've found out happening below solution future reference , hoping it's able other desperate souls me:
my mapping entirely wrong. material_pedido has no relationship pedido (not sure agree design, but...) , entity framework not approve creation of relationships not exist on database. there third table that, besides containing materialpedido's children, relationship between pedido , materialpedido.
[i can't post images, won't able post db model =(]. cleared relationships , started remodeling scratch.
here working relationships:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.entity<pedido>() .haskey(p => new { p.cliente, p.codigo }) .hasmany<detalhepedido>(p => p.detalhespedido) .withrequired(dp => dp.pedido) .hasforeignkey(dp => new { dp.cliente, dp.nrpedido }); modelbuilder.entity<materialpedido>() .haskey(mp => new { mp.nrpedido, mp.cliente, mp.codigo }) .hasmany<detalhepedido>(mp => mp.detalhespedido) .withrequired(dp => dp.materialpedido) .hasforeignkey(dp => new { dp.nrpedido, dp.cliente, dp.material }); } [table("material_pedido")] public class materialpedido : ivalidatableobject { [key, required, column(order = 1)] public int nrpedido { get; set; } [key, required, column(order = 2), foreignkey("dadoscliente")] public int cliente { get; set; } public virtual cliente dadoscliente { get; set; } [key, column(order = 3)] public string codigo { get; set; } public virtual icollection<detalhepedido> detalhespedido { get; set; } } [table("detalhe_pedido")] public class detalhepedido { [key, column(order = 1)] public int nrpedido { get; set; } [key, column(order = 2)] public int cliente { get; set; } [key, column(order = 3)] public string material { get; set; } [key, column(order = 4)] public string codigo { get; set; } public virtual pedido pedido { get; set; } public virtual materialpedido materialpedido { get; set; } } [table("pedido")] public class pedido : ivalidatableobject { [key, column(order = 1), displayname("cód pedido"), required] [databasegenerated(databasegeneratedoption.identity)] public int codigo { get; set; } [key, column(order = 2), required, customvalidation(typeof(granitoentities), "notzero")] public int cliente { get; set; } [foreignkey("cliente")] public virtual cliente dadoscliente { get; set; } public virtual icollection<detalhepedido> detalhespedido { get; set; } }
Comments
Post a Comment