[英]Getting an error in Entity Framework Core
EF Core : what can I do if I get this error in the project I am working on: EF Core:如果我在我正在处理的项目中遇到此错误,我该怎么办:
MySqlException: Duplicate entry '2' for key 'customers.IX_Customers_UserID' MySqlException: 键“customers.IX_Customers_UserID”的重复条目“2”
UserID
causes this error when it is present - I do not understand exactly what the error is, please help 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; }
public int UserID { get; set; }
}
public class Supplier
{
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",
User = db.Users.FirstOrDefault(x => x.ID == 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");
}
}
}
Your problem most probably is running on these lines of code:您的问题很可能在这些代码行上运行:
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(); // ERROR happening here, when trying to insert Users
}
The error is happening because you have put a unique PRIMARY Key constraint over the UserId
column.发生错误是因为您在UserId
列上放置了唯一的 PRIMARY Key 约束。
Try adding the following attribute over the ID, to fix the issue:尝试在 ID 上添加以下属性,以解决问题:
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
...
}
Another note, I think the UserId
property redundant in your Customer
class.另一个注意事项,我认为UserId
属性在您的Customer
类中是多余的。
Please also check that if your User⟶Customer relations are defined as one-to-many, then in your Customer
class you have to define a collection of Users
but not a single User
:还请检查您的 User⟶Customer 关系是否定义为一对多,那么在您的Customer
类中,您必须定义一组Users
而不是单个User
:
public class Customer
{
...
public ICollection<User> Users { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.