实体框架代码第一循环依赖 [英] Entity Framework Code First Circular Dependices

查看:161
本文介绍了实体框架代码第一循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



看一下



在简单的例子中您提供的实体框架不应该有任何抱怨,如果您删除博客和BlogId属性在邮政,但可能还有其他更复杂的情况。



实体将默认添加一个外键中的数据库行中的拥有博客行,但域模型只会是n



以下片段将首先从数据库加载第一个博客,然后延迟加载该博客的帖子。

  using(var db = new BlogContext())
{
var blog = db.Blogs.FirstOrDefault();

// lazy加载在前一行中获取的博客的帖子
foreach(var post in blog.Posts)
{
Trace.TraceInformation(string .Format(帖子{0}的标题是{1},post.Id,post.Title));
}
}
}

如果您真的需要导航从博客到帖子和从帖子到博客,由于您的应用程序中有一些业务需求,那么您将必须在查询和双向关联之间进行决定。
选择哪一个完全取决于您,实体框架同时支持。



双向关联是指当您有双向导航属性时,使您能够在两个方向导航。即如果博客有一个帖子属性,并且每个帖子都有一个博客属性指向博客,那么我们有一个双向关联。



另一种选择是拥有导航属性只在一边。例如。博客可能包含帖子列表,这将使您可以轻松地从博客导航到帖子,这可能是最需要的。
如果您在某些用例中引用了一篇文章,并且需要找出它所属的博客,那么您可以通过在您的存储库/ dbcontext上查询查找博客来搜索博客对象,该对象具有发布在其帖子列表中。



如果需要相反方向的导航,DDD的倡导者通常会推荐单向导航和查询。


I am a bit confused with Circular Dependices with EF as its seems like everything will just become a circular dependency.

Looking at this tutorial

public class Blog
{
    public int BlogId { get; set; }
    public string Name { get; set; }

    public virtual List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public virtual Blog Blog { get; set; }
}

When I run the "code map" in Vs2012 I do indeed see that it is a circular reference

Should I not be worried about this? I was trying to use Autofixture to generate dummy data but it crashes because of circular references.

解决方案

I guess it depends on who you ask if you should worry or not about bidirectional associations or not. In domain driven design people usually say you should worry about this. E.g. Eric Evans say in his book Domain driven design: tackling the complexity in the heart of software you should avoid them if possible.

Julie Lerman writes about the issue in a recent post here

In the simple example you provided entity framework should not have any complains if you removed the Blog and BlogId properties on Post, but there may be other more complex cases.

Entity will by default add a Foreign key in the Posts rows in the database to the owning Blogs row, but the domain model will only be navigable from Blogs to Posts.

The following snippet will first load the first Blog from the database, and then lazy load the Posts of that blog.

using (var db = new BlogContext())
{
    var blog = db.Blogs.FirstOrDefault();

    //lazy loading the Posts of the blog that was fetched in previous line        
    foreach (var post in blog.Posts)
    {
        Trace.TraceInformation(string.Format("Title of post {0} is {1}",  post.Id, post.Title));
        }
    }
}

If you really need to navigate both from Blog to Posts and from Posts to Blogs, because of some business requirement in your application, then you will have to decide between querying and a bi-directional association. Which one to choose is completely up to you, Entity Framework supports both.

A bi-directional association is when you have a navigational property on both sides, enabling you to navigate in both directions. I.e. if Blog has a Posts property and each Post has a Blog property pointing back to the Blog, then we have a bi-directional association.

An alternative option is to have a navigational property on only one side. E.g. a Blog may contain a list of Posts, this would make it easy to navigate from Blog to Posts which is probably what is most of needed. If you in some use case have a reference to a Post and need to find out which Blog it belongs to, then you could find the blog by doing a query on your repository/dbcontext searching for a Blog object that has the post in its lists of Posts.

Advocates of DDD often recommends uni-directional navigation and queries if navigation in the opposite direction is needed.

这篇关于实体框架代码第一循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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