我的实体框架预编译 LINQ 类抛出 System.TypeInitializationException
我有一个用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在单元测试(或只是一个方法)中尝试静态字段(上下文初始化和查询编译)中的代码,并查看实际的异常及其堆栈跟踪。
此外,将 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.)