В Мой 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);
Что я забыл сделать?
Update: Я также попытался добавить [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