.net - The model backing the <Database> context has changed since the database was created -
the error message :
"the model backing 'addressbook' context has changed since database created. either manually delete/update database, or call database.setinitializer idatabaseinitializer instance. example, recreatedatabaseifmodelchanges strategy automatically delete , recreate database, , optionally seed new data."
i trying use code-first feature , following wrote:
var modelbuilder = new modelbuilder(); var model = modelbuilder.createmodel(); using (addressbook context = new addressbook(model)) { var contact = new contact { contactid = 10000, firstname = "brian", lastname = "lara", modifieddate = datetime.now, adddate = datetime.now, title = "mr." }; context.contacts.add(contact); int result = context.savechanges(); console.writeline("result :- "+ result.tostring()); }
the context class:
public class addressbook : dbcontext { public addressbook() { } public addressbook(dbmodel addressbook) : base(addressbook) { } public dbset<contact> contacts { get; set; } public dbset<address> addresses { get; set; } }
and connection string:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionstrings> <add name="addressbook" providername="system.data.sqlclient" connectionstring="data source=mymachine;initial catalog=addressbook; integrated security=true;multipleactiveresultsets=true;"/> </connectionstrings> </configuration>
so, database name "addressbook" , error happens when trying add contact object context. missing here?
now it's:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { database.setinitializer<yourdbcontext>(null); base.onmodelcreating(modelbuilder); }
in yourdbcontext.cs file.
Comments
Post a Comment