Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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#_Entity Framework_Asp.net Mvc 5 - Fatal编程技术网

C# 为什么实体框架给我一个关于不存在的数据的错误

C# 为什么实体框架给我一个关于不存在的数据的错误,c#,entity-framework,asp.net-mvc-5,C#,Entity Framework,Asp.net Mvc 5,我正在使用mvc5和实体框架代码优先迁移 我尝试为表设置种子,但收到以下错误消息: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Orders_dbo.Users_UserId". The conflict occurred in database "DbDemo.Models.DbbContext", table "dbo.Users", column 'Id'. The statement

我正在使用mvc5和实体框架代码优先迁移 我尝试为表设置种子,但收到以下错误消息:

The INSERT statement conflicted with the FOREIGN KEY constraint 
"FK_dbo.Orders_dbo.Users_UserId". The conflict occurred in database 
"DbDemo.Models.DbbContext", table "dbo.Users", column 'Id'.
The statement has been terminated.
我知道有人说,在播种订单之前,数据必须已经存在于users表中,但users中的
userid
是一个主键,所以我认为它是自动播种的,因为它的identity=true!那我为什么会犯这个错误呢

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string PhoneNum { get; set; }
    public string Location { get; set; }
    public string Type { get; set; }
}

public class Order
{
    [Key]
    [Column(Order = 1)]
    public int Id { get; set; }

    //ForeignKey from Items table for the composite key
    [Key]
    [Column(Order = 2)]
    public int? ItemId { get; set; }
    public Item Item { get; set; }

    //ForeignKey from Users table
    public int? UserId { get; set; }
    public User User { get; set; }

    public int Quantity { get; set; }
    public int? TotalPrice { get; set; } //why did i put this field??!
}
这是用户种子迁移:

public partial class UsersSeed : DbMigration
{
    public override void Up()
    {
        Sql("insert into Users (Name,Email,Password,PhoneNum,Location,Type) values ('Khaled Tahboub', 'khaledk.tahboub@gmail.com', 'abc@123', '0797504280', 'Amman', 'A')");
        Sql("insert into Users (Name,Email,Password,PhoneNum,Location,Type) values ('Muath Mustafa', 'muathmustafa@gmail.com', 'abc@123', '0797504280', 'Amman', 'A')");
        Sql("insert into Users (Name,Email,Password,PhoneNum,Location,Type) values ('John Doe', 'JohnDoe@gmail.com', 'abc@123', '0797504280', 'Amman', 'B')");
    }

    public override void Down()
    {
    }
}
public partial class OrdersSeed : DbMigration
{
    public override void Up()
    {
        Sql("insert into Orders (UserId, ItemId, Quantity) values (1, 1, 1)");
    }

    public override void Down()
    {
    }
}
这是种子迁移的顺序:

public partial class UsersSeed : DbMigration
{
    public override void Up()
    {
        Sql("insert into Users (Name,Email,Password,PhoneNum,Location,Type) values ('Khaled Tahboub', 'khaledk.tahboub@gmail.com', 'abc@123', '0797504280', 'Amman', 'A')");
        Sql("insert into Users (Name,Email,Password,PhoneNum,Location,Type) values ('Muath Mustafa', 'muathmustafa@gmail.com', 'abc@123', '0797504280', 'Amman', 'A')");
        Sql("insert into Users (Name,Email,Password,PhoneNum,Location,Type) values ('John Doe', 'JohnDoe@gmail.com', 'abc@123', '0797504280', 'Amman', 'B')");
    }

    public override void Down()
    {
    }
}
public partial class OrdersSeed : DbMigration
{
    public override void Up()
    {
        Sql("insert into Orders (UserId, ItemId, Quantity) values (1, 1, 1)");
    }

    public override void Down()
    {
    }
}

这意味着您试图插入一个用户ID在用户表中不存在的订单。您是否试图同时创建订单和新用户?请出示你用来做这项工作的代码。我刚刚在帖子中提供了代码。这是第一个代码吗?用户类缺少将Id定义为键/标识的属性,您也缺少指示实体框架订购的属性。UserId是对Users.Id的FK。