好的,你可以在下面看到我的簡化模型。如果省略映射,我會在我的基礎數據庫中生成各種重複密鑰BusinessCardActions表每次我添加一個新的集合,如請求或邀請在下面的示例代碼。CodeFirst生成重複的外鍵 - EntityFramework
// DbContext
public DbSet<BusinessCardAction> BusinessCardActions { get; set; }
// Classes
public abstract class BusinessCardAction
{
public Int32 Id { get; set; }
public User User { get; set; }
public Int32 UserId { get; set; }
public Int32 OtherUserId { get; set; }
public User OtherUser { get; set; }
public Guid UniqueId { get; set; }
public String Email { get; set; }
public String OtherEmail { get; set; }
public Int32 UserAction { get; protected set; }
public DateTime ActionDate { get; set; }
}
public abstract class StatusAction : BusinessCardAction
{
public Int32 Status { get; set; }
public String Password { get; set; }
}
public class Request : StatusAction
{
public Int32 InviteId { get; set; }
}
public class Acceptance : BusinessCardAction
{
}
public class Nudge : BusinessCardAction
{
}
public class User
{
public Int32 Id { get; set; }
public ICollection<Associate> AssociatedUsers { get; set; }
public ICollection<Request> Requests { get; set; }
public ICollection<Invite> Invites{ get; set; }
public ICollection<BusinessCardAction> RecentActions { get; set; }
}
現在,我沒有,如果有隻是沒有辦法共享外鍵列,在我的基類,派生類中有這些重複列一個巨大的問題。但這不是發生的事情。以下插入後的鏈接中顯示的數據顯示它們全部共享所需的UserId列。
我創建了一些測試代碼,並在添加數據後添加了結果鏈接。
var accept = new Acceptance()
{
Email = "[email protected]",
OtherUserId = 2,
UserId = 1,
OtherEmail = "[email protected]",
Associate = "James",
ActionDate = DateTime.Now
};
var invite = new Invite()
{
Email = "[email protected]",
OtherUserId = 2,
UserId = 1,
OtherEmail = "[email protected]",
Status = 1,
Password = "Password",
ActionDate = DateTime.Now
};
var request = new Request()
{
Email = "[email protected]",
OtherUserId = 2,
UserId = 1,
OtherEmail = "[email protected]",
Password = "********",
Status = 2,
ActionDate = DateTime.Now
};
var nudge = new Nudge()
{
Email = "[email protected]",
OtherUserId = 2,
UserId = 1,
OtherEmail = "[email protected]",
ActionDate = DateTime.Now
};
_businessCardActionService.Data.BusinessCardActions.Add(accept);
_businessCardActionService.Data.BusinessCardActions.Add(invite);
_businessCardActionService.Data.BusinessCardActions.Add(request);
_businessCardActionService.Data.BusinessCardActions.Add(nudge);
_businessCardActionService.Save();
請點擊此鏈接查看BusinessCardActionsData前面插入後 http://screencast.com/t/JKBcpaCT742
因此,大家可以看到,針對不同類型存儲在基礎類表。不幸的是,表格末尾有一些無用的列,看起來沒用。我一直在試圖映射它們,以便這些列在生成的代碼中消失。
這裏是類的映射部分。
...
modelBuilder.Entity<Nudge>()
.HasRequired(i => i.User)
.WithMany()
.HasForeignKey(i => i.UserId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Acceptance>()
.HasRequired(a => a.User)
.WithMany()
.HasForeignKey(a => a.UserId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Nudge>()
.HasRequired(i => i.OtherUser)
.WithMany()
.HasForeignKey(i => i.OtherUserId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Acceptance>()
.HasRequired(a => a.OtherUser)
.WithMany()
.HasForeignKey(a => a.OtherUser)
.WillCascadeOnDelete(false);
而不是一個很好的生成數據庫表我只碰到下面的錯誤,這是有道理的,以一定的程度,但在同一時間以某種方式惹惱了我深深的。也許有辦法做到這一點,但我還沒有找到它。
在'Ellevate.BusinessCards.Models.BusinessCardAction'類型上聲明的導航屬性'User'已配置爲具有衝突的多重性。
你在得到什麼? – IronMan84