0

I would like to use the new migration feature of EF, but on my local machine when I simulate a Medium Trust environment adding "trust level=Medium" on the web.config file, I get this security exception:

   System.Reflection.RuntimeMethodInfo.GetMethodBody() +24
   System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext) +1031
   System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) +66
   System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +52
   System.Data.Entity.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c) +75
   System.Data.Entity.Internal.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6() +19
   System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +72
   System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +185
   System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +7
   System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +118
   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +190
   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +73
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +28
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +57
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
   System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +37
   System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +63

EF Migrations work in Full Trust only?

4
  • What is it exactly you want to know?
    – Steven
    Mar 31, 2012 at 10:53
  • I want to know if migrations can't works in medium trust or I have some problems on my machine.
    – Luca CB
    Mar 31, 2012 at 13:56
  • 1
    Update your question to make this clear and add any information about the exception or error you are getting.
    – Steven
    Mar 31, 2012 at 16:43
  • Is there any chance that you could reupload this patched EF 5.0 dll? Right now there is 404.
    – narcos
    Aug 31, 2012 at 15:07

3 Answers 3

2

The first EF migrations CTP mentioned the Full trust limitation with note that it can be addressed in future release but no future release mentioned this limitation as fixed so it most probably still exists.

3
  • Does Entity Framework Code First Migrations still require Full Trust on version 4.3 release?
    – IsmailS
    May 28, 2012 at 7:40
  • 1
    @iSid: The question was for version 4.3 and release notes for 4.3.1 didn't mentioned this as fixed so I assume yes. May 28, 2012 at 9:04
  • So if I don't use automatic migration and I generate a migration script locally and execute it on server, then Full Trust wont be required on server. Am I correct in assuming that?
    – IsmailS
    May 30, 2012 at 5:05
2

To your consideration: I've just discovered what was the problem in my case with EF and medium trust environment.

After reading this article and making some code revision I discovered that one of my EF model class aggregates another class that has private properties. It seems that EF tries to load those properties, which is not allowed for it in medium trust environment. Removing this reference from my model class (I copy needed properties instead now) resolved my problem which was resulting in ReflectionPermission exception.

If this won't help, there is another option which some people claim that it helped them. Add this attribute at the beggining of your model file or in AssemblyInfo.cs:

[assembly:AllowPartiallyTrustedCallers]

Read more about it here.

0

You can replace the following code in System.Data.Entity.Migrations.DBMigrator class constructor:

if (method != null)
{
    this._hasSeedLogic = method.GetMethodBody().GetILAsByteArray().Length > 2;
}

to:

if (method != null)
{
    this._hasSeedLogic = true;
}

with Reflector and Reflexil plugin. After this hack it will work in Medium trust environment.

If you trust me, you can download the patched dll (EF 5.0 beta 2) from here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.