Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 首先在代码中创建额外的键_C#_Asp.net Mvc_Entity Framework_Ef Code First - Fatal编程技术网

C# 首先在代码中创建额外的键

C# 首先在代码中创建额外的键,c#,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net Mvc,Entity Framework,Ef Code First,我遇到了一个问题,在我的代码第一次迁移中会生成额外的字段,而且我不希望出现这种关系。我有一个基本的模型,彩票车 [Table("lottery_carts")] public class LotteryCart : Auditable { public LotteryCart() { LotteryCartItems = new List<LotteryCartItem>(); } //stored in banner: no

我遇到了一个问题,在我的代码第一次迁移中会生成额外的字段,而且我不希望出现这种关系。我有一个基本的模型,彩票车

[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只是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");
我不知道为什么彩票车上会有class_Id和class_Id 1,这似乎没有直接的关系

提前感谢您的帮助

应正确定义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; }
}

您使用的是哪个版本的EF?嗯,主键字段不应该为空@AppDeveloper版本5。哇,我改了!还是没有快乐。我最初有一个专用的密钥字段,当我意识到它不需要时,我忘记了取消空值@JordanBrooklyn我在我的电脑上运行了它,它运行得很好。迁移不包含最后四行。谢谢您的帮助!
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; }
}