识别关键在我的EF6代码第一种模式,我有以下类:EF不是关系
class User : IdentityUser // so it has public string Id!
{
public virtual long ItemId { get; set; }
public virtual Item Item { get; set; }
}
class Item
{
public virtual long Id { get; set;
public virtual string UserId { get; set; }
public virtual User User { get; set; }
}
,并在我的情况下,我做了以下内容:
OnModelCreating(DbModelBuilder mB)
{
mB.Entity<User>().HasOptional(x => x.Item).WithOptionalPrincipal(x => x.User).WillCascadeOnDelete(false);
}
但是,我的迁移产生以下内容:
CreateTable(
"dbo.Items",
c => new
{
Id = c.Long(nullable: false, identity: true),
UserId = c.String(),
User_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Users", t => t.User_Id)
.Index(t => t.User_Id);
我忘记了什么?
更新:我也尝试添加[ForeignKey("User")]
到Item.UserId
UPDATE2: 我发现了一个解决办法,但它不是东西,是很...
OnModelCreating(DbModelBuilder mB)
{
mB.Entity<User>().HasOptional(x => x.Item).WithMany().HasForeignKey(x => x.ItemId);
mB.Entity<Item>().HasOptional(x => x.User).WithMany().HasForeignKey(x => x.UserId);
}
为什么你只有虚拟财产? – jannagy02
只是由类图生成的,稍后会更改 – Nefarion
您是试图定义一对一还是一对一,还是一对一,还是实际上想要一对一-许多? http://msdn.microsoft.com/en-us/data/jj591620.aspx – Colin