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

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

问题描述

我试图在Entity Framework中创建一个多对多的关系(代码第一),根据以下文章:



然而,我无法让它正常工作,我确信我做错了的方法非常简单。这是我没有尝试的图:





连接表的要点是我需要在关系中有一个额外的属性Level,所以我不能随便顾问与计划之间的直接关系。我在设计器中手动添加了ConsultantProgramLink实体,然后分别向程序和顾问添加关联,选择为每个添加一个FK,然后将它们作为主键。但是当我这样做的时候,它的工作不如我所预期的那样:



如果我在顾问和计划之间做了直接的关联,我本来可以参考在我的代码中,请咨询顾问。但是现在连接表不行。有什么办法可以补救这个问题,还是我总是要经过交界属性(Consultant.ConsultantProgramLink.Programs)?在任何情况下,即使我尝试通过交汇处的财产,这没有帮助。我可以在我的代码中做Consultant.ConsultantProgramLink,但另一个点不给我导航属性程序(由于某些原因也变为简单的程序,为什么?如果我最终可以访问它们,可以重命名它们)? 。



那么我做错了什么?为什么我无法通过代码中的点符号来访问属性?

解决方案

将连接表建模为实体后,确实在顾问程序之间失去了直接的多对多关系。那就是它的工作原理您将在连接表中具有直接的多对多关系或附加属性。不是两个如果您希望两者都可以尝试在顾问上创建自定义程序属性,并使用linq查询获取相关程序: p>

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

这个例子也是你最后一个问题的解释。您不能在 ConsultantProgramLink 上的程序属性,因为它是相关实体的集合,而不是单个实体被称为 ConsultantProgramLinks )。 ConsultantProgramLink 实体中的属性简称为程序,因为它代表单个实体不是集合。

$ b $ X- 2004545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 200新X- 20045 X- 20045 CEEC X-新评新新新新新旗新新旗旗新新新新旗新新旗旗新200 200 200 200 200 200 200 CE 200 200 200 200 200 200 200 CE 2004545 X-
...
context.Programs.AddObject(program);

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

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

context.SaveChanges();

如果您添加新的顾问以相同的方式创建到所有程序的链接。 200的X- 200 X- 200 X- 200 X- 200 200 X- 200 200 X- 200 200 X- 200 200 X- 200 200 X- 1992。为避免这种情况,唯一的选择是在程序表中使用存储过程或触发器。


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:

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:

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?).

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

解决方案

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);   
    }
}

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.

Edit:

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.

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天全站免登陆