简体   繁体   English

EF Code首先,不是第二实体的ID单向多对多关系

[英]EF Code first, unidirectional many to many relationship not by Id of second entity

This is my first time asking something in Stack, and I've already searched a lot w/o any luck. 这是我第一次在Stack中提出问题,而且我已经进行了很多搜索,没有任何运气。

I'm using EF 4 and I want to create a many to many relationship using code first. 我正在使用EF 4,我想先使用代码创建多对多关系。 The problem I have is that the application is localized and has currently two languages (Spanish and English) Because of that I've created two entities: Practitioner and Speciality: 我的问题是该应用程序已本地化,并且当前具有两种语言(西班牙语和英语),因此我创建了两个实体:Practitioner和Speciality:

[Table("Practitioners")]
public class PractitionerModel
{
   [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int Id { get; set;}
    public ICollection<SpecialityModel> Specialities { get; set; }
}

[Table("Specialities")]
public class SpecialityModel
{
    public virtual int Id { get; set;}
    public string Name { get; set; }
    public int Code { get; set; }
}

I want a Practitioner to have many Specialities and want the "Mapping" to happen by Code not by Id Since there are two Specialities with the same code for example: Ginecology and Ginecologia" They are the same speciality and have code 12 with different Ids. 我希望从业人员有很多专长,并且希望通过代码而不是由ID进行“映射”,因为有两个专长具有相同的代码,例如:Ginecology和Ginecologia。它们是同一专长,并且具有不同ID的代码12。

The idea is that the data in the db should look like: 这个想法是数据库中的数据应如下所示:

-----------------                                        ------------------------------------------
| Practitioner |                                         |     Specialities                       |
----------------      ------------------------------     ------------------------------------------    
| Id = 10      |--   | PractitionerToSpecialities |   -- | Id = 1, Code = 12, Name = "Ginecology" | 
----------------  |  ------------------------------  |   ------------------------------------------
                   --| PracId = 10, SpecCode = 12 |----- | Id = 2, Code = 12, Name = "Ginecologia"|
                      -----------------------------      ----------------------------------------    --

Is there any way to acchieve this with CodeFirst? 有什么办法可以通过CodeFirst实现这一目标?

Globalization should not affect your database design in this way. 全球化不应以这种方式影响您的数据库设计。

It would be much better to simply have one specialty record per specialty (thus the relationship could be by id), and then corresponding resources (either in the .NET resource manager or as fields in the specialty table) for displaying the name in different languages. 最好只为每个专业创建一个专业记录(这样的关系可以通过id进行),然后使用相应的资源(在.NET资源管理器中或作为专业表中的字段)以不同的语言显示名称会更好。 。

That said, try something like this: 也就是说,请尝试如下操作:

[Table("Practitioners")]
public class PractitionerModel
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int Id { get; set;}

    [ForeignKey("Specialties")]
    public virtual int Code {get; set; }
    public ICollection<SpecialityModel> Specialities { get; set; }
}

[Table("Specialities")]
public class SpecialityModel
{
    public virtual int Id { get; set;}
    public string Name { get; set; }
    public int Code { get; set; }
}

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

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