2013-10-13 14 views
36

Oto przypadek, mam 2 podmioty, takie jak umowy, media.Jak utworzyć mapowanie wiele do wielu w Entity Framework?

public class Media : Entity 
{ 
    public string Name {get; set;} 
    public bool Enabled 
    *//other properties can be ignored..* 
} 

public class Contract : Entity 
{ 
    public string Code {get; set;} 
    *//other properties can be ignored..* 
} 

Umowa ma wiele Medias, wydaje się, że są one wiele do wielu.

Ale !! w ef kodu najpierw potrzebuję 3 więcej pól w tabeli ContractMedia (ef auto generowane). , takie jak StartDate, EndDate i Price. nie można ich dodać w encji Media.

Jak mapować w tej sprawie?

+0

Możliwy duplikat [Utwórz kod pierwszy, wiele do wielu, z dodatkowymi polami w tabeli Association] (http://stackoverflow.com/questions/7050404/create-code-first-many-to-many- with-dodatkowe-pola-w-asocjacji-tabeli) – forsvarir

Odpowiedz

73

Jeśli chcesz utworzyć wiele do wielu relacji z dodatkowymi danymi w tabeli powiązań, musisz utworzyć tabelę powiązań jako encję. Czysty związek wiele do wielu jest tylko w czystej tabeli z identyfikatorem podmiotu.

W ty przypadku będzie to:

public class Media // One entity table 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool Enabled { get; set; } 

    public virtual ICollection<ContractMedia> ContractMedias { get; set; } 
} 

public class Contract // Second entity table 
{ 
    public int Id { get; set; } 
    public string Code { get; set } 

    public virtual ICollection<ContractMedia> ContractMedias { get; set; } 
} 

public class ContractMedia // Association table implemented as entity 
{ 
    public int MediaId { get; set; } 
    public int ContractId { get; set; } 
    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 
    public double Price { get; set; } 

    public virtual Media Media { get; set; } 
    public virtual Contract Contract { get; set; } 
} 

A po utworzeniu modeli/podmioty, trzeba określić relacje w kontekście:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<ContractMedia>() 
     .HasKey(c => new { c.MediaId, c.ContractId }); 

    modelBuilder.Entity<Contract>() 
     .HasMany(c => c.ContractMedias) 
     .WithRequired() 
     .HasForeignKey(c => c.ContractId); 

    modelBuilder.Entity<Media>() 
     .HasMany(c => c.ContractMedias) 
     .WithRequired() 
     .HasForeignKey(c => c.MediaId); 
} 

Ponadto można odnieść się do tych linków:
Many to many mapping with extra fields in Fluent API
Entity Framework CodeFirst many to many relationship with additional information
Create code first, many to many, with additional fields in association table

+5

Myślę, że 'ContractMedia' nie powinien mieć odwrotnych kolekcji nav:' Medias' & 'Contracts'. Powinny to być właściwości forward nav. Otrzymałem dodatkowe pola w tabeli wielu-wielu odnośników, dopóki nie zmieniłem właściwości odwrotnych (kolekcji) na właściwości forward. – IAbstract

+0

@Abstract Myślę, że możesz mieć rację, ponieważ nie pobieram wartości z nawigacji do wspólnej tabeli. I jestem pewien, że jest to spowodowane kolekcjami nav –

+0

@Abstract: Jak zmienić "właściwości odwrotne (kolekcja), aby przekazać właściwości."? – eugenekgn

1

Dodanie do @ Tomas odpowiedzi bez konieczności korzystania z Fluent API.

public class Media // One entity table 
{ 
    public int Id { get; set; } 

    public string Name { get; set; } 

    public virtual ICollection<ContractMedia> ContractMedias { get; set; } 
} 

public class Contract // Second entity table 
{ 
    public int Id { get; set; } 

    public string Code { get; set } 

    public virtual ICollection<ContractMedia> ContractMedias { get; set; } 
} 

public class ContractMedia // Association table implemented as entity 
{ 
    [Key] 
    [Column(Order = 0)] 
    [ForeignKey("Media")] 
    public int MediaId { get; set; } 

    [Key] 
    [Column(Order = 1)] 
    [ForeignKey("Contract")] 
    public int ContractId { get; set; } 

    public DateTime StartDate { get; set; } 

    public DateTime EndDate { get; set; } 

    public double Price { get; set; } 

    public virtual Media Media { get; set; } 

    public virtual Contract Contract { get; set; } 
} 
Powiązane problemy