许多人在实体框架与结表中的许多关系? [英] Many to many relationship with junction table in Entity Framework?

查看:221
本文介绍了许多人在实体框架与结表中的许多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建实体框架(code在前)一个多一对多的关系,按照下面的帖子:<一href=\"http://stackoverflow.com/questions/5153849/database-design-for-limited-number-of-choices-in-mvc-and-entity-framework\">Database设计的MVC和Entity Framework选择有限数量?

I'm trying to create a many-to-many relationship in Entity Framework (code first), according to the following post: Database design for limited number of choices in MVC and Entity Framework?

不过,我不能让它正常工作,我敢肯定,我在做一些非常简单的错误的方式。下面是图我没有从我的尝试:

However, I can't get it to work properly, and I'm sure I'm doing something very simple the wrong way. Here's the diagram I have no from my attempts:

联接表的一点是,我需要有一个额外的属性,等级,关系,所以我只是顾问和项目之间的直接关系不走。我在设计手动添加ConsultantProgramLink实体,再分别加入协会计划和顾问,选择要添加一个FK每个,然后让他们两个主键。但是,当我不喜欢这样,我希望它不工作:

The point of the junction table is that I need to have an extra property, Level, in the relationship, so I can't just go with a direct relationship between Consultant and Program. I added the ConsultantProgramLink entity manually in the designer, and then added associations to Program and Consultant respectively, selecting to add a FK for each, and then made them both primary keys. But when I do it like this it doesn't work as I expected:

如果我做了顾问和项目之间的直接联系,我本来可以参考,比如说,Consultant.Programs在我的code。但是,这并不与接线表现在的工作。有什么办法来解决这个问题,还是我总是要经过路口财产(Consultant.ConsultantProgramLink.Programs)?在任何情况下,即使我尝试去通过路口属性它并不能帮助。我可以在我的code做Consultant.ConsultantProgramLink,但另一点不给我的导航属性的程序(由于某种原因也成为简单的程序,为什么呢?可我只是将其重命名,如果我最终在访问它们所有?)。

If I had done a direct association between Consultant and Program, I would have been able to refer to, say, Consultant.Programs in my code. But that doesn't work now with the junction table. Is there any way to remedy this, or do I always have to go through the junction property (Consultant.ConsultantProgramLink.Programs)? In any case, even if I do try to go through the junction property it doesn't help. I can do Consultant.ConsultantProgramLink in my code, but another dot doesn't give me the navigation property Programs (which for some reason also became simply Program, why? Can I just rename them if I eventually get access to them at all?).

那么,我做错了什么?为什么我不能访问通过点符号的属性在我的code?

So what am I doing wrong? Why can't I access the properties through dot notation in my code?

推荐答案

一旦你结表建模为一个实体,你确实失去直接不少一对多顾问和程序。这是如何工作的。你或者可以在结合表直接不少一对多关系或其他属性。不能同时使用。如果你想同时你可以尝试在顾问和使用LINQ查询创建自定义的程序属性来获取相关的程序:

Once you model a junction table as an entity you indeed lose direct many-to-many relation between Consultant and Program. That is how it works. You will either have direct many-to-many relation or additional properties in the junction table. Not both. If you want both you can try creating custom Programs property on Consultant and use linq query to get related programs:

public IEnumerable<Program> Programs
{
    get
    {
        return this.ConsultantProgramLinks.Select(l => l.Program);   
    }
}

这个例子也是你的最后一个问题的解释。你不能对 ConsultantProgramLink 程序属性,因为它是相关实体的集合,而不是单一的实体(应该被称为 ConsultantProgramLinks )。在属性 ConsultantProgramLink 实体被简单地称为程序,因为它重新presents单一的实体不会收集。

The example is also the explanation of your last problem. You can't have Program property on ConsultantProgramLink because it is a collection of related entities, not single entity (it should be called ConsultantProgramLinks). The property in ConsultantProgramLink entity is called simply Programbecause it represents single entity not collection.

编辑:

如果你需要每个程序每个顾问,您必须执行它时,你会被自动关联创建新的程序。公开为单独的实体有结表将可能让你很容易地实现它:

If you need each Program to be automatically associated with each Consultant you must enforce it when you are going to create new Program. Having junction table exposed as separate entity will probably allow you achieving it easily:

var program = new Program();
...
context.Programs.AddObject(program);

var ids = from c in context.Consultants
          select c.Id;

foreach (var id in ids)
{
    var link = new ConsultantProgramLink
        {
            ConsultantId = id,
            Program = program
        };
    context.ConsultantProgramLinks.AddObject(link);
}

context.SaveChanges();

如果您添加新的顾问你必须创建指向同样的方式的所有程序。

If you add new Consultant you will have to create links to all programs in the same way.

缺点是,如果你有如10顾问这个结构将创建每个刀片将在单独的往返到数据库中执行1001数据库插入。为了避免它唯一的选择就是要么使用存储在节目表再修改的或触发器。

The disadvantage is that if you have for example 1000 consultants this construct will create 1001 database inserts where each insert will be executed in separate roundtrip to the database. To avoid it the only option is either use stored procedur or trigger on Program table.

这篇关于许多人在实体框架与结表中的许多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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