Search code examples
c#entity-frameworklinqpad

using LinkPad 5 I am unable to load a EF6 custom dll and using entities


I have attempted to access an existing custom DbContext from my solution, using LinqPad 5, and use that test\develop queries and code. (I had achieved the same 2 years ago in a similar project)

I am able to create the connection, and it tests successfully but when I try to access it in a query I consistently receive the following error.

configuration system failed to initialize inner exception: SerializationException Type 'EMTPassportDomain.Supermodel.Persistance.EMTPassportDbContext' in assembly 'EMTPassportDomain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable

Due to our use of an in house framework I have to set several things up but this is what a query looks like (at least based on my earlier successful attempt with a similar project)

void Main()
{
    var serializerSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;

    var contractResolver = (DefaultContractResolver)serializerSettings.ContractResolver;
    contractResolver.IgnoreSerializableAttribute = true;

    SupermodelInitialization.Init<EMTPassportDbContext>(new CustomRepoFactory(), webApiAuthFilter: new EMTPassportBasicHttpAuthenticateAttribue());
//    InitializerManager.InitPerConfig(new List<IDatabaseInitializer<EMTPassportDbContext>>
//    {
//        new EMTPassportCreateDatabaseIfNotExists(),
//        new EMTPassportDropCreateDatabaseIfModelChanges(),
//        new EMTPassportDropCreateDatabaseAlways()
//    });

    using (new EMTPassportUnitOfWork(ReadOnly.Yes))  //EMTPassportUnitOfWorkIfNoAmbientContext
    {
        var userRepo = (EMTUserRepo)RepoFactory.Create<EMTUser>();
        var test = userRepo.Items;//.Where(u => u.GovEmail == GovEmail && u.Id != Id);
        test.Take(1).ToList().Dump();
    };
}

I've included most names spaces, copied the dll.config file over into LinqPad.query.config I'm just not sure how to proceed....

I did get this working a couple of years ago after some futsing but I just don't know how to move forward and I miss my LinqPad.

Sorry if this isn't clear, this is my first stackoverflow question ever... and thanks in advance.


Solution

  • Lol, only took 8 years to figure it out, the solution is to make this call

    using (var context = new EMTPassportDbContext())    {context.Database.Initialize(force: false); }
    

    before doing anything else with the DbContext. The Initialize with the force forces the DbContext into a fully realized state before I make any additional calls. Now I don't know why this works but it does!

    For me I put the call right after the

    SupermodelInitialization.Init<EMTPassportDbContext>(new CustomRepoFactory(), webApiAuthFilter: new EMTPassportBasicHttpAuthenticateAttribue());
    

    call as that lined does some set up as well.

    So, I'm back to being able to test Linq calls with my live DbContext!