我的实体框架预编译 LINQ 类抛出 System.TypeInitializationException

发布于 2024-11-06 18:23:16 字数 1809 浏览 0 评论 0原文

我有一个用于 LINQ 扩展方法的类。我从以下类中收到 System.TypeInitializationException

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>>> 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;
    }

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>>> 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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫思念不要吵 2024-11-13 18:23:16

在单元测试(或只是一个方法)中尝试静态字段(上下文初始化和查询编译)中的代码,并查看实际的异常及其堆栈跟踪。

此外,将 DataContext 放在静态字段中也是一个定时炸弹。在每个方法中创建一个新方法或共享它,将其存储在受控位置(如 HttpContext.Item、Call.Context 等)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文