c# - EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship -
c# - EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship -
i'm attempting following:
public class class1() { public int id {get;set;} [foreignkey("class2")] public int class2id {get;set;} public virtual class2 class2 {get;set;} } public class class2() { public int id {get;set;} [required] public virtual int class1id {get;set;} [required] [foreignkey("class1id")] public class1 class1 {get;set;} }
however every time seek migrate database next error:
class1_class2_target: : multiplicity not valid in role 'class2_class1_target' in relationship 'class2_class1'. because dependent role properties not key properties, upper bound of multiplicity of dependent role must '*'.
what issue here?
your model not 1:1 association. can still have many class2
objects referring same one class1
object. also, model doesn't guarantee class2
referring class1
referred class1
object — class1
can refer class2
object.
the mutual way guarantee (sort of) 1:1 association in sql have table principal entity , 1 dependent entity primary key in dependent table foreign key principle:
(here class1
principle)
now in relational database, still doesn't guarantee 1:1 association (that's why said 'sort of'). it's 1:0..1 association. there can class1
without class2
. truth is, genuine 1:1 associations impossible in sql, because there no language build inserts 2 rows in different tables synchronously. 1:0..1 closest get.
fluent mapping
to model association in ef can utilize fluent api. here's standard way it:
class class1map : entitytypeconfiguration<class1> { public class1map() { this.haskey(c => c.id); this.property(c => c.id) .hasdatabasegeneratedoption(databasegeneratedoption.identity); this.hasrequired(c1 => c1.class2).withrequiredprincipal(c2 => c2.class1); } }
and in context:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.configurations.add(new class1map()); }
and left of classes:
public class class1() { public int id {get;set;} public virtual class2 class2 {get;set;} } public class class2() { public int id {get;set;} public virtual class1 class1 {get;set;} }
there no way configure alternate foreign key properties in model, because fk involved has be dependent's primary key.
the unusual thing model ef doesn't stop creating (and saving) class1
object without class2
. think ef should capable of validating requirement before saving changes, but, apparently, doesn't. likewise, there ways delete class2
object without deleting class1
parent. hasrequired
- withrequired
pair not stringent looks (and should be).
data annotations
the way right in code info annotations. (of course of study database model still not able enforce 1:1)
public class class1() { public int id {get;set;} [required] public virtual class2 class2 {get;set;} } public class class2() { [key, foreignkey("class1")] public int id {get;set;} [required] public virtual class1 class1 {get;set;} }
the [key, foreignkey("class1")]
annotation tells ef class1
principle entity.
data annotations play role in many apis, can curse, because each api chooses own subset implement, here comes in handy, because ef not uses them design info model, validate entities. if seek save class1
object without class2
you'll validation error.
c# database entity-framework ef-code-first entity-framework-6
Comments
Post a Comment