2012-03-29 64 views
1

我有兩個表(表A,表B)與一個有3個有效負載列的連接表(TableAB)連接。 By Payload我指的是除了Id,TableAId和TableBId以外的列。M:M映射 - EF 4.3 CodeFirst(現有數據庫)

我可以成功插入到所有表中,但我需要將數據插入Insert上的有效載荷列之一。我使用EF 4.3,Fluent API。誰能幫忙?提前致謝。

public class Organisation : EntityBase<int>, IAggregateRoot 
     { 
    public string Name { get; set; } 
    public string Url { get; set; } 
    public int CountryId { get; set; } 
    public int? OwnershipTypeId { get; set; } 
    public int OrganisationStatusId { get; set; } 

    public virtual ICollection<Feature> Features { get; set; } 
    public virtual ICollection<OrganisationType> OrganisationTypes { get; set; } 
    public virtual ICollection<PricePlan> PricePlans { get; set; } 
    public virtual ICollection<User> Users { get; set; } 

}

public class User: EntityBase<Guid>, IAggregateRoot 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string JobTitle { get; set; } 
    public int? PhoneCallingCodeId  { get; set; } 
    public int? PhoneAreaCode{ get; set; } 
    public string PhoneLocal { get; set; } 
    public int? MobileCallingCodeId { get; set; } 
    public int? MobileAreaCode { get; set; } 
    public string MobileLocal { get; set; }  

    public virtual ICollection<Organisation.Organisation> Organisations { get; set; } 

} 

    public class OrganisationUser : EntityBase<int>, IAggregateRoot 
{ 
    public DateTime StartDate { get; set; } 
    public DateTime? EndDate { get; set; } 
    public int OrganisationRoleId {get; set;}//Foreign Key - have tried leaving it out, tried it as public virtual Organisation Organisation {get;set; 
    public bool IsApproved { get; set; } 
} 

    public class SDContext : DbContext 
{  

    public ObjectContext Core 
    { 
     get 
     { 
      return (this as IObjectContextAdapter).ObjectContext; 
     } 
    } 
    public IDbSet<User> User { get; set; } 

    public IDbSet<Organisation> Organisation { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

     modelBuilder.Entity<Organisation>().HasMany(u => u.Users).WithMany(o => o.Organisations).Map(m => 
     { 
      m.MapLeftKey("OrganisationId"); 
      m.MapRightKey("UserId"); 
      m.ToTable("OrganisationUser"); 
     }); 

//我試圖明確界定用流利的外鍵,但我真的需要明白,一旦我進入我如何添加有效載荷性能和編輯它們。

+1

能否請你告訴你的類(尤其是他們的導航屬性)和Fluent映射? – Slauma 2012-03-29 15:20:01

+0

嗨,這真的很重要嗎?我知道這些表與POCO正確映射,因爲ID全部插入正常。我只是不知道如何訪問有效載荷列。比方說,我有一個組織,我添加用戶,賬單計劃等,所以我很想得到相關實體,在foreach循環中使用organisation.Add(entity)將這些集合添加到我的組織中。我如何訪問有效負載的連接表 - 我已經爲連接創建了一個POCO類。 – user1182263 2012-03-29 18:39:42

+0

是的,這很重要,因爲圍繞你的問題的漫長沉默證明了這一點。由於您沒有提供必要的詳細信息,因此您至今沒有答案。最後一句是很重要的 - 你的連接表的實體,這將有你的有效載荷列,對不對?那麼問題在哪裏?加載它,改變它,將它保存...您可以編輯您的問題(「編輯」鏈接下面的問題)來添加更多的相關信息。 – Slauma 2012-03-29 18:50:27

回答

2

您的映射對於您的目的不正確。如果你想OrganisationUser之間對待OrganisationUser作爲中間實體必須創建OrganisationOrganisationUser之間和UserOrganisationUser,不能直接與OrganisationUser之間的關係。

由於中間實體包含自己的標量屬性,因此您無法創建多對多映射。 EF不支持「有效負載」的多對多關係。你需要兩個一個一對多的關係:

public class Organisation : EntityBase<int>, IAggregateRoot 
{ 
    // ... 
    // this replaces the Users collection 
    public virtual ICollection<OrganisationUser> OrganisationUsers { get; set; } 
} 

public class User : EntityBase<Guid>, IAggregateRoot 
{ 
    // ... 
    // this replaces the Organisations collection 
    public virtual ICollection<OrganisationUser> OrganisationUsers { get; set; } 
} 

public class OrganisationUser : EntityBase<int>, IAggregateRoot 
{ 
    public int OrganisationId { get; set; } 
    public Organisation Organisation { get; set; } 

    public Guid UserId { get; set; } 
    public User User { get; set; } 

    // ... "payload" properties ... 
} 

用流利的API,您必須通過以下取代許多一對多映射:

modelBuilder.Entity<Organisation>() 
    .HasMany(o => o.OrganisationUsers) 
    .WithRequired(ou => ou.Organisation) 
    .HasForeignKey(ou => ou.OrganisationId); 

modelBuilder.Entity<User>() 
    .HasMany(u => u.OrganisationUsers) 
    .WithRequired(ou => ou.User) 
    .HasForeignKey(ou => ou.UserId); 

派生的DbContext也可以包含單獨的一組爲OrganisationUser實體:

public IDbSet<OrganisationUser> OrganisationUsers { get; set; } 

很明顯,你現在怎麼寫的東西到中間表:

var newOrganisationUser = new OrganisastionUser 
{ 
    OrganisationId = 5, 
    UserId = 8, 
    SomePayLoadProperty = someValue, 
    // ... 
}; 

context.OrganisastionUsers.Add(newOrganisastionUser); 
context.SaveChanges(); 

如果你想確保每對OrganisationIdUserId可以在鏈接表只存在一次,這將是更好地使那些兩列的複合主鍵,以確保在數據庫中,而不是唯一使用單獨的Id。在流利的API將是:

modelBuilder.Entity<OrganisationUser>() 
    .HasKey(ou => new { ou.OrganisationId, ou.UserId }); 

更多關於這種類型的模型,以及如何與它合作的細節是在這裏:

Create code first, many to many, with additional fields in association table

+0

感謝一家工廠!我今天會嘗試。我之前讀過這篇文章,但我很驚訝,現在在EF中還沒有這方面的支持。非常感謝幫忙。 – user1182263 2012-03-30 06:38:24

+0

嗨,這工作。我仍然有問題,但它是因爲我用它試圖提供一個ID爲我的身份規範列EntityBase。真的很感謝幫助。謝謝。 – user1182263 2012-03-30 14:24:11

+0

院校佈局結構調整我的情況,這種情況下,我得到了錯誤的SQLException:外鍵「FK_dbo.OrganisationUser_dbo.Organisation_OrganisationId」中引用的表「dbo.Organisation」引用無效列「OrganisationId」。無法創建約束。爲什麼? – 2012-10-16 15:22:09