实体框架4.1 code-首先,需要延迟加载的参考是节省空 [英] Entity Framework 4.1 code-first, required lazy load reference is null on save

查看:132
本文介绍了实体框架4.1 code-首先,需要延迟加载的参考是节省空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个论坛项目ASP.NET MVC 3,实体框架的最新版本的.NET上运行4

I'm building a forum project for ASP.NET MVC 3, running on .NET 4 with the latest version of Entity Framework.

我平时的设计论坛,与类别一局,并与论坛,论坛主题与主题,并与文章分类等。

I have the usual forum design, with a Board with Categories, and Categories with Forums, Forums with Topics and Topics with Posts etc.

简体:

public class Category {
    [Required]
    public virtual Board Board { get; set; }
}
public class Forum {
    [Required]
    public virtual Category Category { get; set; }
}
public class Topic {
    [Required]
    public virtual Forum Forum { get; set; }
}
public class Post {
    [Required]
    public virtual Topic Topic { get; set; }
}

当创建一个新的职位的话题被告知,当一个话题被改变,论坛告知。

When a new post is created the topic is informed, and when a topic is changed, the forum is informed.

所以,再简化为:

public class Forum {
    public void TopicChanged(Topic topic) {
        // Do stuff
    }
}
public class Topic {
    public void PostAdded(Post post) {
        // Do stuff
        this.Forum.TopicChanged(this);
    }
}

所以,创建一个新的帖子(和它提交到数据库)后,我打电话PostAdded父话题。现在,这是它获得奇!

So after creating a new Post (and committing it to the database), I call PostAdded on the parent topic. Now this is where it get odd!

当我更改提交到我的仓库,我得到验证错误,在论坛类别是必需的。

When I commit the changes to my repositories, I get a validation error, Category on Forum is required.

如果我期待在数据库中,该论坛有对父类的引用。如果我停止code和着眼于论坛对象,它有一个类别实例。因此,这看起来像延迟加载的问题,特别是因为如果我加入这一行:

If I look in the database, the Forum has a reference to the parent Category. If I stop the code and looks at the Forum object, it has a Category instance. So this looks like an issue with lazy loading, especially because if I add this line:

var cat = this.Category

目前在论坛类TopicChanged方法的底部,不再有任何错误。

At the bottom of the TopicChanged method in the Forum class, there's no longer any errors.

我做得完全错在这里,有我碰到一个边缘问题,或者是怎么回事?我想通EF会看到引用是延迟加载的参考,如果还没有改变,还有为什么这应该保存失败没有理由???

Am I doing something totally wrong here, have I run into a borderline issue, or what is going on? I figured EF would see that the reference is a lazy loaded reference, and if it hasn't been changed, there's no reason why this should fail on save???

谢谢,
斯蒂恩

Thanks, Steen

推荐答案

我已经解决我的问题。它可能不是一个非常漂亮和干净的解决方案,但至少我现在可以处理这种情况,而不必改变了很多我的code(这需要与NHibernate也跑)。因此,没有肮脏的解决方案。

I've fixed my problem. It might not be a really nice and clean solution, but at least I can handle this situation now, without having to change a lot of my code (which needs to run with nHibernate also). So no dirty solution.

只是为了给你它是如何解决的,我会试着在这里解释它的想法。

Just to give you an idea on how it's solved, I'll try and explain it here.

在我的库中的commit()方法,我开始上的DbContext实例调用GetValidationErrors()。这将返回我上面发生错误的实体而遇到的错误。在此基础上这一类型的实体(实体是EF生成的代理)通过我的所有属性上运行,当我遇到必需属性的属性,我呼吁的GetValue上的代理对象相同的属性。

On the Commit() method on my repository, I start off calling GetValidationErrors() on the DbContext instance. This returns the error I encountered above along with the entity where the error occurs. On the base type of this entity (the entity is a proxy generated by EF) I run through all properties and when I encounter a property with the Required attribute, I call GetValue on the identical property on the proxy object.

少废话,更code:

var errors = this.context.GetValidationErrors();
foreach (DbEntityValidationResult result in errors) {
    Type baseType = result.Entry.Entity.GetType().BaseType;
    foreach (PropertyInfo property in result.Entry.Entity.GetType().GetProperties()) {
        if (baseType.GetProperty(property.Name).GetCustomAttributes(typeof(RequiredAttribute), true).Any()) {
            property.GetValue(result.Entry.Entity, null);
        }
    }
}

这篇关于实体框架4.1 code-首先,需要延迟加载的参考是节省空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆