简体   繁体   中英

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.

I'm using EF 4 and I want to create a many to many relationship using code first. 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:

[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.

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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