开发者

My Entity Framework precomiled LINQ class is throwing a System.TypeInitializationException

I have a class that is used for LINQ extension methods. I am getting a System.TypeInitializationException from the following class:

public static class CarLib {
    private readonly static IOPTS_EFEntities Context = new myEFEntities();

    public IQueryable<Transmission> GetTransmissions(this Car car, IEFEntities context = null) {
        if (context == null) 
        context = Context;

        return SelectCarWithTransmissionCompiled(car, context);
    }

    public IQueryable<Tire> GetTires(this Car car, IEFEntities context=null) {
         if (context == null) 
         context = Context;

         return SelectTiresCompiled(car, context);
    }

    private static readonly Func<Car, IEFEntities, IQueryable<Transmission>> SelectTransmissionsCompiled = SelectTransmissions.Compile();
    private static readonly Func<Car, IEFEntities, IQueryable<Tire>> SelectTiresCompiled = SelectTires.Compile();

    private static readonly Expression<Func<Car, IEFEntities, IQueryable<Transmission>>> SelectTransmissions= (myCar,context) => 
                    from trans in context.Transmissions
                    where trans.CarId == myCar.Id 
                    select trans;

    private static readonly Expression<Func<Car, IEFEntities, IQueryable<Tire>>> 开发者_JAVA百科SelectTires = (car,context) =>
                    from tire in context.Tires
                    where car.Id == tire.CarId
                    && accused.Ssn.IsValidSsn()
                    && !tire.Brand.IsNullEmptyOrWhitespace()
                    && !tire.Diameter.IsNullEmptyOrWhitespace()
                    && !tire.Weight.IsNullEmptyOrWhitespace()
                    && tire.Price.IsPopulated()
                    select tire;
    }


Try the code you have in the static fields (context initialization and compilation of the queries) in a unit test (or just a method) and see the actual exception and its stack trace.

Also, having the DataContext in an static field is a time bomb. Create a new one in each method or share it storing it in a controlled place (like the HttpContext.Item, Call.Context, etc.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜