I have a table of users, where a user can be the manager of another user (this is optional, thus int? IdManager
)
[Table("users")]
public partial class User
{
#region Properties
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
[Required]
[Column("Id")]
public int Id { get; set; }
#endregion
#region DataNavigation
/// <summary>
/// Navigation Id for getting parent Field
/// WARNING: this works, but need to remove foreign key creation in migration file !
/// </summary>
[ForeignKey("Manager")]
[Column("IdManager")]
public int? IdManager { get; set; }
/// <summary>
/// Navigation object to parent Field
/// </summary>
[ScriptIgnore]
[JsonIgnore]
[IgnoreDataMember]
public virtual User Manager { get; set; }
#endregion
}
Now in code first, the above code generates this
public partial class manager : DbMigration
{
public override void Up()
{
AddColumn("users", "IdManager", c => c.Int());
CreateIndex("users", "IdManager");
AddForeignKey("users", "IdManager", "users", "Id");
}
public override void Down()
{
DropForeignKey("users", "IdManager", "users");
DropIndex("users", new[] { "IdManager" });
DropColumn("users", "IdManager");
}
}
However it breaks while updating the database with Cannot add foreign key constraint
But if i delete the foreign key creation
public partial class manager : DbMigration
{
public override void Up()
{
AddColumn("users", "IdManager", c => c.Int());
CreateIndex("users", "IdManager");
}
public override void Down()
{
DropIndex("users", new[] { "IdManager" });
DropColumn("users", "IdManager");
}
}
Everything works fine in dbContext and Linq
...but, i have to leave [ForeignKey("Manager")]
on my user object (for linq & dbcontext), which is not so good, since at a later time it will certainly blow up in someones face again.
Any idea on how i can map this properly either via Data Attributes or fluent api ?
Thanks!