asp.net mvc - MVC5 EF6.1 Code First Foreign Key Constraint -


public class names {     [key]     public int nameid { get; set; }     [stringlength(100)]     [index(isunique = true)]     [required]     public string name { get; set; } }  public class people {     [key]     public int peopleid { get; set; }     [required]     public int firstnameid { get; set; }     [foreignkey("firstnameid")]     public names firstname { get; set; }     public int middlenameid { get; set; }     [foreignkey("middlenameid")]     public names middlename { get; set; }     public int lastnameid { get; set; }     [foreignkey("lastnameid")]     public names lastname { get; set; }     public datetime dateofbirth { get; set; } } 

after creating migration , trying update database, i'm getting following error:

introducing foreign key constraint 'fk_dbo.people_dbo.names_lastnameid' on table 'people' may cause cycles or multiple cascade paths. specify on delete no action or on update no action, or modify other foreign key constraints. 

could not create constraint or index. see previous errors.

i added following context, based on other articles i've seen online, did not wanted to.

    protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         modelbuilder.conventions.remove<manytomanycascadedeleteconvention>();     } 

here's resulting migration code...

namespace mydataset.migrations {     using system;     using system.data.entity.migrations;      public partial class peoplenameids : dbmigration     {         public override void up()         {             createtable(                 "dbo.names",                 c => new                     {                         nameid = c.int(nullable: false, identity: true),                         name = c.string(nullable: false, maxlength: 100),                     })                 .primarykey(t => t.nameid)                 .index(t => t.name, unique: true);              createtable(                 "dbo.people",                 c => new                     {                         peopleid = c.int(nullable: false, identity: true),                         firstnameid = c.int(nullable: false),                         middlenameid = c.int(nullable: false),                         lastnameid = c.int(nullable: false),                         dateofbirth = c.datetime(nullable: false),                     })                 .primarykey(t => t.peopleid)                 .foreignkey("dbo.names", t => t.firstnameid, cascadedelete: true)                 .foreignkey("dbo.names", t => t.lastnameid, cascadedelete: true)                 .foreignkey("dbo.names", t => t.middlenameid, cascadedelete: true)                 .index(t => t.firstnameid)                 .index(t => t.middlenameid)                 .index(t => t.lastnameid);          }          public override void down()         {             dropforeignkey("dbo.people", "middlenameid", "dbo.names");             dropforeignkey("dbo.people", "lastnameid", "dbo.names");             dropforeignkey("dbo.people", "firstnameid", "dbo.names");             dropindex("dbo.people", new[] { "lastnameid" });             dropindex("dbo.people", new[] { "middlenameid" });             dropindex("dbo.people", new[] { "firstnameid" });             dropindex("dbo.names", new[] { "name" });             droptable("dbo.people");             droptable("dbo.names");         }     } } 

my goal have "people" table of *nameids foreign keys names table. however, firstname required. when first tested out, , had foreign key on firstnameid, worked fine. when copy/pasted same other names, that's i'm @ now.

any , appreciated. thank much.

cascade delete can turned off fluent configuration. however, have tried

modelbuilder.conventions.remove<manytomanycascadedeleteconvention>(); 

has no effect, because have no many-to-many relationship, 3 one-to-many relationships. instead, should configure each relationship separately:

modelbuilder.entity<people>()     .hasrequired(p => p.firstname)     .withmany()     .hasforeignkey(p => p.firstnameid)     .willcascadeondelete(false);  modelbuilder.entity<people>()     .hasrequired(p => p.middlename)     .withmany()     .hasforeignkey(p => p.middlenameid)     .willcascadeondelete(false);  modelbuilder.entity<people>()     .hasrequired(p => p.lastname)     .withmany()     .hasforeignkey(p => p.lastnameid)     .willcascadeondelete(false); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -