visual studio 2012 - EF5, code first exception ONLY on first database access: "FK_x...x' is not a constraint. Could not drop constraint" -


this has been driving me crazy days. using vs2012, ef5 code first, sql server 2012. on development machine , i below exception when, , when, hit database first time. if, before run tests, init database, throws below exception, but not thereafter. if run tests, first test fails because of exception, but other tests pass. if catch exception , ignore it, all tests pass without error. frustrating, i've defined constraints, between 2 tables, don't know 'users_id' defined. i've tried manually deleting tables , running vs2012 generated sql dbcontext recreate table fresh, nothing works. appreciated.

here exception

*system.data.sqlclient.sqlexception: 'fk_dbo.userprofiles_dbo.users_id' not constraint. not drop constraint. see previous errors.*

here pocos

user

public class user : entity<user>, iuser    {       public guid id { get; set; }       // nav property userprofile.       public virtual userprofile userprofile { get; set; }       public guid siteid { get; set; }       public virtual site site { get; set; }    } 

userprofile

public class userprofile : entity<userprofile>, iuserprofile    {       // points user.id       public guid id { get; set; }       // nav property user.       public virtual user user { get; set; }    } 

here's code first definition of 2 tables (other code left out brevity):

edit: relationship between 2 tables id user.id => userprofile.id

user

modelbuilder.entity<user>().haskey(p => p.id);          modelbuilder.entity<user>().hasrequired(p => p.site).withmany(p => p.users).hasforeignkey(p => p.siteid);          modelbuilder.entity<user>()                    .hasrequired(p => p.userprofile)                    .withrequiredprincipal(p => p.user)                    .willcascadeondelete(true); 

userprofile

modelbuilder.entity<userprofile>().haskey(p => p.id);          modelbuilder.entity<userprofile>().hasrequired(p => p.user); 

this fixed it

in dbcontext

protected override void onmodelcreating(dbmodelbuilder modelbuilder)       {          database.setinitializer(new migratedatabasetolatestversion<context, contextconfiguration>());           base.onmodelcreating(modelbuilder);       } 

my migration configuration

public class contextconfiguration : dbmigrationsconfiguration<context>    {       public contextconfiguration()       {          this.automaticmigrationsenabled = true;          this.automaticmigrationdatalossallowed = true;       }    } 

in tests

[testinitialize]       public override void testinitialize()       {          // force model updates.          using (var uow = unitofworkfactory.instance.create<unitofworkcore>(defaultconnectionstring))          {             uow.database.initialize(force: false);          }           // begin transaction          this.transactionscope = new transactionscope();           this.unitofwork = unitofworkfactory.instance.create<unitofworkcore>(defaultconnectionstring);       } 

i changed things , not no problems *i changed things , not no problems* i changed things , not no problems

in tests

[testinitialize]       public override void testinitialize()       {          // force model updates.           // changed this:          database.setinitializer(new dropcreatedatabasealways<context>());           using (var uow = unitofworkfactory.instance.create<unitofworkcore>(defaultconnectionstring))          {             uow.database.initialize(force: false);          }           // begin transaction          this.transactionscope = new transactionscope();           this.unitofwork = unitofworkfactory.instance.create<unitofworkcore>(defaultconnectionstring);       } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -