The following entities is my code first which PersonParameter is an abstract class and Shirt and Shoes are inherited from it with typ(1,2)
[Table("Person")]
public class Person
{
[Key]
public int id { get; set; }
public string Name { get; set; }
public int? shirtID { get; set; }
public int? shoesID { get; set; }
[ForeignKey("shirtID")]
public Shirt Shirt { get; set; }
[ForeignKey("shoesID")]
public Shoes Shoes { get; set; }
}
[Table("PersonParameter")]
public abstract class PersonParameter
{
public int id { get; set; }
public string Title { get; set; }
public string Value { get; set; }
public List<Person> Persons { get; set; }
}
public class Shirt : PersonParameter
{
}
public class Shoes : PersonParameter
{
}
and for model dbcontext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<PersonParameter>()
.Map<Shirt>(p => p.Requires("typ").HasValue(1))
.Map<Shoes>(p => p.Requires("typ").HasValue(2));
}
but the above codes will create unwanted field PersonParameter_id in Person table:
public override void Up()
{
CreateTable(
"dbo.PersonParameter",
c => new
{
id = c.Int(nullable: false, identity: true),
Title = c.String(),
Value = c.String(),
typ = c.Int(nullable: false),
})
.PrimaryKey(t => t.id);
CreateTable(
"dbo.Person",
c => new
{
id = c.Int(nullable: false, identity: true),
Name = c.String(),
shirtID = c.Int(),
shoesID = c.Int(),
PersonParameter_id = c.Int(),
})
.PrimaryKey(t => t.id)
.ForeignKey("dbo.PersonParameter", t => t.shirtID)
.ForeignKey("dbo.PersonParameter", t => t.shoesID)
.ForeignKey("dbo.PersonParameter", t => t.PersonParameter_id)
.Index(t => t.shirtID)
.Index(t => t.shoesID)
.Index(t => t.PersonParameter_id);
}
how can I solve it (PersonParameter_id) I did some FluentApi with HasOptional.WithMany but didn't solve.
EDIT
After some test, found that for non abstract class it create extra id too and the one solution for fixing that issue is removing navigation property from parameter class and adding it to the inheritance classes (shirt and shoes)