我有兩個表(表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");
});
//我試圖明確界定用流利的外鍵,但我真的需要明白,一旦我進入我如何添加有效載荷性能和編輯它們。
能否請你告訴你的類(尤其是他們的導航屬性)和Fluent映射? – Slauma 2012-03-29 15:20:01
嗨,這真的很重要嗎?我知道這些表與POCO正確映射,因爲ID全部插入正常。我只是不知道如何訪問有效載荷列。比方說,我有一個組織,我添加用戶,賬單計劃等,所以我很想得到相關實體,在foreach循環中使用organisation.Add(entity)將這些集合添加到我的組織中。我如何訪問有效負載的連接表 - 我已經爲連接創建了一個POCO類。 – user1182263 2012-03-29 18:39:42
是的,這很重要,因爲圍繞你的問題的漫長沉默證明了這一點。由於您沒有提供必要的詳細信息,因此您至今沒有答案。最後一句是很重要的 - 你的連接表的實體,這將有你的有效載荷列,對不對?那麼問題在哪裏?加載它,改變它,將它保存...您可以編輯您的問題(「編輯」鏈接下面的問題)來添加更多的相關信息。 – Slauma 2012-03-29 18:50:27