I have an MVC3 app with EDMX model:

<add name="MFIModelContainer" connectionString="metadata=res://MFI/Models.MFIModel.csdl|res://MFI/Models.MFIModel.ssdl|res://MFI/Models.MFIModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=VILLAIN\SQLEXPRESS;initial catalog=SISTEM;User ID=sa; Password=pasword;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Sometimes i have need to do some dynamic stuff (parse strings, create linq expressions, compile and call).

I have very simple function:

    public static decimal Payment(Order sd, string arg)
    {
        MFIModelContainer mf = new MFIModelContainer();
        Kredit k = mf.Dokumenti.OfType<Kredit>().Single(a => a.Ugovor == sd.IntProperty2);
        DataModelContainer db = new DataModelContainer();            
        List<KreditOtplata> ko = mf.KreditOtplate.Where(a => a.Kredit == k.IdDokument).ToList();

        // Some calculations...

        if (arg == "R") return sd.Amount.Value * 80m / 100m;
        else return sd.Amount.Value * 20m / 100m;
    }

I call this function as a part of a linq expression (something like following):

Type common = Assembly.LoadFile("path to assembply").GetType("MFI.Models.Common");
MethodInfo mi = common.GetMethod("Payment", BindingFlags.Static | BindingFlags.Public);
Expression call = Expression.Call(mi, Expression.Parameter(typeof(Order)), Expression.Constant(typeof(string)));
return Expression.Lambda(SelectExpr, Expression.Parameter(typeof(Order))).Compile().DynamicInvoke(instance_of_Order);

After the call, if i instantiate MFIModelContainer "normally" (in regular code) I get "multiple mapping error".

Also, if i first instantiate MFIModelContainer "normally", and then via linq expressions again i get the error.

If i use one method exclusivelly, everything will be ok.

So, I presume, EDMX was loaded twice, but i dont know what to do about it.

I also must stress that this EDMX is not defined in primary dll of my MVC app, but in additional one, that is hooked up in global.asax (i feel that this might be important).

Could anyone get me some pointers what should i do to resolve this?

Best regards,

1

There are 1 answers

0
Milos Mijatovic On

Ok, just to answer my question.

It turns out that

Assembly.LoadFile() 

is not very good at resolving dependencies.

So, I switched to

Assembly.LoadFrom()

and all is all right.

Regards.