2013-10-13 17 views
36

Dưới đây là trường hợp, tôi có 2 thực thể, chẳng hạn như hợp đồng, Media.Cách tạo ánh xạ nhiều đối tượng trong Khuôn khổ thực thể?

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..* 
} 

Hợp đồng có nhiều Medias, có vẻ như họ rất nhiều để nhiều người.

Nhưng !! tại mã ef đầu tiên, tôi cần thêm 3 trường trong bảng ContractMedia (ef auto generated). chẳng hạn như Ngày bắt đầu, Ngày kết thúc và Giá. chúng không thể được thêm vào thực thể Media.

Cách ánh xạ trong trường hợp này ??

+0

Có thể trùng lặp [Tạo mã đầu tiên, nhiều thành nhiều, với các trường bổ sung trong bảng liên kết] (http://stackoverflow.com/questions/7050404/create-code-first-many-to-many- with-additional-fields-in-association-table) – forsvarir

Trả lời

73

Nếu bạn muốn tạo nhiều mối quan hệ với dữ liệu bổ sung trong bảng kết hợp, bạn phải tạo bảng kết hợp làm thực thể. Mối quan hệ nhiều đến nhiều thuần túy chỉ ở trong bảng thuần túy với id của thực thể.

Trong bạn trường hợp nó sẽ là:

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

Và sau khi bạn tạo mô hình/đơn vị, bạn cần xác định mối quan hệ trong bối cảnh:

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

Ngoài ra bạn có thể tham khảo những liên kết này:
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

Tôi nghĩ rằng 'ContractMedia' không nên có các bộ sưu tập đảo ngược nav:' Medias' & 'Contracts'. Những thay vào đó nên được chuyển tiếp tài sản nav. Tôi đã nhận thêm các trường trong bảng tra cứu nhiều người cho đến khi tôi thay đổi thuộc tính nghịch đảo (tập hợp) để chuyển tiếp các thuộc tính. – IAbstract

+0

@IAbstract Tôi nghĩ bạn có thể đúng vì tôi không lấy giá trị từ điều hướng đến bảng chung. Và tôi chắc chắn nó là do các bộ sưu tập nav –

+0

@IAbstract: Làm thế nào để bạn thay đổi "nghịch đảo (bộ sưu tập) tài sản để chuyển tiếp tài sản."? – eugenekgn

1

Thêm vào @Tomas trả lời mà không phải sử dụng API thông thạo.

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; } 
} 
Các vấn đề liên quan