In My EF6 Code First model, i have the following classes:
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; }
}
and in my Context, i do the following:
OnModelCreating(DbModelBuilder mB)
{
mB.Entity<User>().HasOptional(x => x.Item).WithOptionalPrincipal(x => x.User).WillCascadeOnDelete(false);
}
BUT, my Migration produces the following:
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);
What did i forget to do?
Update: I also tried to add [ForeignKey("User")]
to Item.UserId
Update2: I found a workaround, but its not something that is pretty...
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);
}