Getting the following error when trying to run a query against a dbcontext assembly in Linqpad.
InvalidOperationException: The model backing the 'UserQuery' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
Having done a bit of reading it seems that:
Database.SetInitializer<DiaryAssistantContext>(null);
is needed. However this is already in my derived DbContext class.
Can anybody give me a pointer?
LINQPad subclasses your typed data context so you can run queries without referencing the instance. Maybe the SetInitializer method needs the subclassed type.
What happens if you replace this code:
Database.SetInitializer<DiaryAssistantContext>(null);
with this:
typeof (Database).GetMethod ("SetInitializer").MakeGenericMethod (GetType()).Invoke (null, new object[] { null });
?
While an answer has already been accepted, in my case I wanted a solution that was a little more compile-time friendly. The following solution is similar to the example in the accepted answer that uses reflection, but will provide a little extra compile-time checking:
Expression<Action> setInitializerExpression = () => Database.SetInitializer<MyContext>(null);
var setInitializerCall = (MethodCallExpression) setInitializerExpression.Body;
var setInitializerMethodInfo =
setInitializerCall.Method.GetGenericMethodDefinition().MakeGenericMethod(GetType());
setInitializerMethodInfo.Invoke(null, new object[] {null});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With