微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EF核心重复条目异常

如何解决EF核心重复条目异常

EF核心在我正在处理的项目中引发异常

MysqLException:键“ customers.IX_Customers_UserID”的条目“ 2”重复

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",new Address(){FullName = "Xezri Balakisiyev",UserID=2},new Address(){FullName = "Nurane Tarverdiyeva",Body = "Naxcivvan",UserID=3},new Address(){FullName = "Rena heyderova",Body = "Xachmaz",new Address(){FullName = "Memmed bedelov",Body = "Sumqayit",UserID=1}
        };
        using (var db = new Context())
        {
            db.Addresses.AddRange(addresses);
            db.SaveChanges();
            Console.WriteLine("Ishledi");
        }
    }

解决方法

对于每个客户实体框架,使用主键2添加一个新用户。要向ID为2的用户添加关系,您需要在user属性中插入一个用户对象。

var customer = new Customer()
        {

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


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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。