in current project, use entity framework 6.0 alpha3 code first approach. have custom data context takes dbconnection in constructor access it's database. migrations done either in visualstudio or migrationtolatestversion
initializer during runtime.
sample:
public class mystackoverflowsamplecontext : dbcontext { dbset<question> questions { get; set; } dbset<answers> answers { get; set; } public mystackoverflowsamplecontext(dbconnection connection) : base(connection) { } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.hasdefaultschema("crm"); base.onmodelcreating(modelbuilder); } }
i check database model using following
if(_dbcontext.database.compatiblewithmodel())
... is, in case: false.
if run initializer , database isn't available yet, get's created way should , compatiblewithmodel
function returns - expected: true.
now, testing purposes, changed database behind, first slightly, completely. delete column, whole table , _migrationshistory
table.
but whatever do: _dbcontext.database.compatiblewithmodel()
returns true! , when try initialize context, weird errors occur, e.g.: "the table tabanswers exists in database.
" - if doesn't exist anymore.
but when try update restore database: "there no pending updates...
"
is bug?
the behaviour seeing correct entity framework point of view, assume entity framework smarter is.
the way in entity framework determines if model compatible database comparing hash stored context , has stored in __migrationshistory
table. why droping column or table doesn't make database.compatiblewithmodel
return false - hashes still same.
now when deleting __migrationshistory
, making entity framework think using code first existing database approach. moment entity framework assume reponsible keeping database , model in sync. behaviour of database.compatiblewithmodel
method in case depends on value of throwifnometadata
parameter. if throwifnometadata
set true exception thrown if no model metadata found either in model associated context or in database itself. if set false method return true if metadata not found.
Comments
Post a Comment