简体   繁体   English

实体框架和延迟加载的问题

[英]Problem with Entity Framework and Lazy Loading

I am using Entity Framework 4 and am having a few problems with lazy loading. 我正在使用Entity Framework 4,并且在延迟加载方面遇到了一些问题。 I have 3 entities and are each contained in one another. 我有3个实体,每个实体相互包含。 CarSetup will contain a Car entity who will contain an Event entity. CarSetup将包含一个Car实体,该实体将包含一个Event实体。 They are all being lazy loaded. 他们都是懒惰加载。

I have created a simplistic unit test to reproduce the problem. 我创建了一个简化的单元测试来重现该问题。

CarSetup carSetup = carSetupContext.CreateObject<CarSetup>();
Car car = Load("car1");
carSetup.Car = car;

I get a crash when i assign the car to the carsetup object. 将汽车分配给carsetup对象时发生崩溃。 It actually crashes in the Equals method of the Car entity. 实际上,它在Car实体的Equals方法中崩溃。

public override bool Equals(object obj)
{
    if(obj == null)
    {
        return false;
    }

    return this.Event.Equals(((Car)obj).Event);
}

If i quickwatch the entity before the equals method gets called, all sub entities get loaded and the problem does not occur. 如果我在调用equals方法之前快速查看实体,则将加载所有子实体,并且不会发生问题。

When i assign the existing car to the car setup, the framework loads all existing CarSetups for that car and calls my "Equals" method with them. 当我将现有汽车分配给汽车设置时,框架会加载该汽车的所有现有CarSetup,并使用它们调用我的“等于”方法。 However, since lazy loading is enabled, the Event in the Car is null which is normal. 但是,由于启用了延迟加载,因此Car中的事件为空,这是正常现象。 When it tries to access the Event property, NO loading happens and it crashes. 当它尝试访问Event属性时,没有加载发生并且崩溃。 I have checked the "this" property in the equals method and it is of type "System.Data.Entity.DynamicProxies.Car". 我在equals方法中检查了“此”属性,它的类型为“ System.Data.Entity.DynamicProxies.Car”。 I also checked and the EventId Guid is correctly set in the car entity. 我还检查了EventId Guid是否在汽车实体中正确设置。

Anyone have any idea as to what is happening? 有人对发生的事情有任何想法吗?

EDIT: After doing a bit more testing, if I call my equals method manualy: 编辑:经过更多测试后,如果我手动调用equals方法:

car.Equals(car);

Everything works perfectly. 一切正常。 It only happens when the Entity Framework decides to load relationships and automatically calls the Equals method. 仅当实体框架决定加载关系并自动调用Equals方法时,才会发生这种情况。

Thanks 谢谢

It seems that the Event isn't being loaded. 似乎未加载该Event From memory I would have done something like this to ensure that the Events are loaded with the Cars: 从内存中,我将执行以下操作,以确保将事件加载到汽车中:

context.Cars.Include("Events");

You're using a syntax that I'm not familiar with, but look into the Include operator. 您使用的是我不熟悉的语法,但请查看Include运算符。

You seem to use POCO objects with enabled DynamicProxies in your object context, right? 您似乎在对象上下文中使用了启用了DynamicProxies的POCO对象,对吗? It might be possible that by casting obj to Car in your Equals override you cast to your POCO class and not to System.Data.Entity.DynamicProxies.Car which is dynamically derived from Car . 通过将obj强制转换为Equals中的Car可能会将您强制转换为POCO类,而不转换为从Car动态派生的System.Data.Entity.DynamicProxies.Car With that cast you are effectively stripping off the ability of obj (which is probably also of type System.Data.Entity.DynamicProxies.Car like this ) to lazily load navigation properties. 与投你有效地剥离的能力obj (这类型可能也System.Data.Entity.DynamicProxies.Car喜欢this懒洋洋地加载导航性能)。 (Because it's the DynamicProxy which enables lazy loading of POCO entities.) (因为是DynamicProxy允许延迟加载POCO实体。)

Now the problem is that you can't cast to System.Data.Entity.DynamicProxies.Car instead of Car since this type doesn't exist at compile time and is only dynamically generated at runtime. 现在的问题是,您不能强制转换为System.Data.Entity.DynamicProxies.Car而不是Car因为此类型在编译时不存在,并且只能在运行时动态生成。

But if you are using C# 4.0 (Visual Studio 2010) you could try to use dynamic typing with the new dynamic keyword. 但是,如果您使用的是C#4.0(Visual Studio 2010),则可以尝试将动态类型与新的dynamic关键字一起使用。 Your Equals override would look like this: 您的Equals替代看起来像这样:

public override bool Equals(object obj)
{
    if(obj == null)
    {
        return false;
    }

    dynamic o = obj;

    return this.Event.Equals(o.Event);
}

(This all is a shot into the dark and I didn't test anything. But it might be worth a try.) (所有这些都是黑暗中的镜头,我没有进行任何测试。但这可能值得一试。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM