Entity Framework 4.1: code first - Foreign key restriction error -
i have base class, let me call, entity_a has property 'id' primarky key.
public abstract class entity_a { [key(), required] [databasegenerated(databasegeneratedoption.identity)] public guid id { get; set; } }
then have 2 other classes, entity_b , entity_c.
entity_b , entity_c inhertis entity_a:
scenario #1:
[table("entity_b")] public class entity_b : entity_a { [required] public virtual string propertyb1 { get; set; } // below foreign key [required] [foreignkey("propertyb2")] public virtual guid propertyb2id { get; set; } public virtual entity_c propertyb2 { get; set; } } [table("entity_c")] public class entity_c : entity_a { [required] public virtual string propertyc1 { get; set; } [required] public virtual string propertyc2 { get; set; } }
it works ok, creates tables. problem when remove entity_a , put 'id' property within each class entity_b , entity_c:
scenario #2:
[table("entity_b")] public class entity_b { [key(), required] [databasegenerated(databasegeneratedoption.identity)] public guid id { get; set; } [required] public virtual string propertyb1 { get; set; } // below foreign key [required] [foreignkey("propertyb2")] public virtual guid propertyb2id { get; set; } public virtual entity_c propertyb2 { get; set; } } [table("entity_c")] public class entity_c { [key(), required] [databasegenerated(databasegeneratedoption.identity)] public guid id { get; set; } [required] public virtual string propertyc1 { get; set; } [required] public virtual string propertyc2 { get; set; } }
when execute, exception raised:
introducing foreign key constraint 'entity_b_propertyb2' on table 'entity_b' may cause cycles or multiple cascade paths. specify on delete no action or on update no action, or modify other foreign key constraints. not create constraint. see previous errors.
i solve overriding onmodelcreating method:
// turn off on delete cascade globaly removing convention protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<onetomanycascadedeleteconvention>(); }
my question are:
1) why entity framework behaviour different scenario #1 (where works) scenario #2 (where not work)?
2) why in scenario #2 not working if not turn off on delete cascade convention? if turn off, practice? wonder if bad or good. also, there way make works without need of overriding onmodelcreating?
thanks lot!
Comments
Post a Comment