EF Core 重复条目异常

EF Core Duplicate entry Exception

EF 核心在我正在处理的项目中抛出异常

MySqlException: Duplicate entry '2' for key 'customers.IX_Customers_UserID'

UserID出现这样的错误我不明白到底是什么错误,请帮忙

public class Context : DbContext
{
    public DbSet<Product> Products { get; set; }

    public DbSet<Category> Categories { get; set; }

    public DbSet<User> Users { get; set; }

    public DbSet<Customer> Customers { get; set; }

    public DbSet<Address> Addresses { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        //optionsBuilder.UseSqlite("Data Source = Shop.db");
        //optionsBuilder.UseSqlServer(@"Data Source = (localdb)\v11.0; Initial Catalog=ShopDb; Integrated Security = SSPI;");
        optionsBuilder.UseMySql(@"server=localhost;port=3306;database=ShopDb1;user=root;password=admin123;");
    }

    public Context()
    {
        Database.EnsureCreated();

    }
}

public class User
{
    public int ID { get; set; }

    public string UserName { get; set; }

    public string Email { get; set; }

    public Customer Customer { get; set; }

    

    public List<Address> Addresses { get; set; }
}

public class Customer
{
    public int ID { get; set; }

    public int IdentifyNumber { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public User User { get; set; }
    [Required]
    public int UserID { get; set; }
}

public class Suplier
{
    public int ID { get; set; }

    public string Name { get; set; }

    public string TaxNumber { get; set; }

}

public class Address
{
    public int ID { get; set; }

    public string FullName { get; set; }

    public string Title { get; set; }

    public string Body { get; set; }


    public User User { get; set; }

    public int UserID { get; set; }
}

public class Product
{


    public int ProductID { get; set; }
    [MaxLength(100)]
    [Required]
    public string Name { get; set; }

    public decimal Price { get; set; }

    public int CategoryID { get; set; }
}

public class Category
{

    public int CategoryID { get; set; }
    [MaxLength(100)]
    [Required]
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {

        using (var db = new Context())
        {
            var customer = new Customer()
            {

                IdentifyNumber = 123,
                FirstName = "Remzi",
                LastName = "Balakishiyev",
                UserID = 2


            };
            db.Customers.Add(customer);
            db.SaveChanges();
        }
        Console.ReadLine();
    }

    static void InsertUsers()
    {
        var users = new List<User> {
            new User() { UserName ="Remzi",Email = "remzi.balakisiyev@gmail.com"},
            new User() { UserName ="Xezri",Email = "xezri.balakisiyev@gmail.com"},
            new User() { UserName ="Nurane",Email = "nurane.tarverdiyeva@gmail.com"}
        };
        using (var db = new Context())
        {
            db.Users.AddRange(users);
            db.SaveChanges();
        }
    }

    static void InsertAddresses()
    {
        var addresses = new List<Address> {
           new Address(){FullName = "Remzi Balakisiyev", Title = "Ev addressi", Body = "Masalli",UserID=1},
           new Address(){FullName = "Remzi Balakisiyev", Title = "Ish addressi", Body = "Baki",UserID=1},
           new Address(){FullName = "Xezri Balakisiyev", Title = "Ev addressi", Body = "Masalli",UserID=2},
           new Address(){FullName = "Nurane Tarverdiyeva", Title = "Ev addressi", Body = "Naxcivvan",UserID=3},
           new Address(){FullName = "Rena Heyderova", Title = "Ev addressi", Body = "Xachmaz",UserID=2},
           new Address(){FullName = "Memmed Bedelov", Title = "Ev addressi", Body = "Sumqayit",UserID=1}
        };
        using (var db = new Context())
        {
            db.Addresses.AddRange(addresses);
            db.SaveChanges();
            Console.WriteLine("Ishledi");
        }
    }

对于每个客户 entity framework 添加一个主键为 2 的新用户。要向 ID 为 2 的用户添加关系,您需要向用户 属性 插入一个用户对象。

var customer = new Customer()
        {

            IdentifyNumber = 123,
            FirstName = "Remzi",
            LastName = "Balakishiyev",
            User = db.Users.FirstOrDefault(x => x.ID == 2)


        };
db.Customers.Add(customer);
db.SaveChanges();