我遇到的问题是,在我的代码首次迁移中生成额外的字段,以及我不想要的关系。我有一个基本型号,LotteryCarts
[Table("lottery_carts")]
public class LotteryCart : Auditable
{
public LotteryCart()
{
LotteryCartItems = new List<LotteryCartItem>();
}
//stored in banner: no
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required(ErrorMessage = "A lottery is required.")]
public int LotteryId { get; set; }
public virtual Lottery Lottery { get; set; }
public virtual List<LotteryCartItem> LotteryCartItems { get; set; }
}
和LotteryCartItem
public class LotteryCartItem
{
[Key, Column(Order=0)]
public int ClassId { get; set; }
public virtual Class Class { get; set; }
[Key, Column(Order = 1)]
public int LotteryCartId { get; set; }
public virtual LotteryCart LotteryCart { get; set; }
public int BidWeight { get; set; }
public int order { get; set; }
public string type { get; set; }
}
LotteryCartItem只是LotteryCart和类之间的连接(带有一些额外的元数据)
出于某种原因,我不明白代码第一次迁移正在生成额外的字段
CreateTable(
"dbo.LotteryCartItems",
c => new
{
ClassId = c.Int(nullable: false),
LotteryCartId = c.Int(nullable: false),
BidWeight = c.Int(nullable: false),
order = c.Int(nullable: false),
type = c.String(),
})
.PrimaryKey(t => new { t.ClassId, t.LotteryCartId })
.ForeignKey("dbo.classes", t => t.ClassId, cascadeDelete: true)
.ForeignKey("dbo.lottery_carts", t => t.LotteryCartId, cascadeDelete: true)
.Index(t => t.ClassId)
.Index(t => t.LotteryCartId);
AddColumn("dbo.lottery_carts", "Class_Id", c => c.Int());
AddColumn("dbo.lottery_carts", "Class_Id1", c => c.Int());
AddForeignKey("dbo.lottery_carts", "Class_Id", "dbo.classes", "Id");
AddForeignKey("dbo.lottery_carts", "Class_Id1", "dbo.classes", "Id");
我不确定为什么lottery_carts上有class_Id和Class_Id1,似乎没有直接关系。
提前感谢您的帮助!
答案 0 :(得分:0)
LotteryCartItem
应该正确定义
public class LotteryCartItem
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
[Key, Column(Order=0), ForeignKey("Class")]
public int ClassId { get; set; }
public virtual Class Class { get; set; }
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
[Key, Column(Order=1), ForeignKey("LotteryCart")]
public int LotteryCartId { get; set; }
public virtual LotteryCart LotteryCart { get; set; }
public int BidWeight { get; set; }
public int order { get; set; }
public string type { get; set; }
}