I have the following model classes:
public class Farmer
{
public int ID { get; set; }
public Box Box { get; set; }
}
public class Apple
{
public int BoxID { get; set; }
public Box Box { get; set; }
public int Number { get { return (int)V2.X; } set { V2 = new Vector2(value, 0); } }
public Vector2 V2 {get;set;}
public Farmer Owner { get; set; }
}
public class Box : IEnumerable<Apple>
{
public int ID { get; set; }
public ICollection<Apple> Apples { get; set; }
public IEnumerator<Apple> GetEnumerator()
{
return Apples.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Apples.GetEnumerator();
}
}
When Box implements IEnumerable<'Apple'>, the shadow property FarmerID is added to the Apple table:
migrationBuilder.AddColumn<int>(
name: "FarmerID",
table: "Apples",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Apples_FarmerID",
table: "Apples",
column: "FarmerID");
migrationBuilder.AddForeignKey(
name: "FK_Apples_Farmers_FarmerID",
table: "Apples",
column: "FarmerID",
principalTable: "Farmers",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
When I comment out the IEnumerable implementation, the expected migration is created without the FarmerID shadow property. Why is this shadow property being generated and how can I remove it? (I've tried ignoring it, but then I end up getting FarmerID1).
I should mention this is my context class:
public class Context : DbContext
{
public DbSet<Farmer> Farmers { get; set; }
public DbSet<Apple> Apples { get; set; }
public DbSet<Box> Boxes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"...");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Apple>()
.HasOne(a => a.Box)
.WithMany(a => a.Apples)
.IsRequired();
modelBuilder.Entity<Apple>()
.Ignore(a => a.V2)
.Ignore(a => a.Owner)
.HasKey(a => new { a.BoxID, a.Number });
}
}