我是C#和.NET的新手,我试图通过本地数据库实现CRUD功能。因此,我创建了一个Product
包含属性和外键的类。但是启动应用程序时出现错误。方法Update()
和Delete()
不起作用,但是Add()
方法可以正常工作。
这是我的通用方法-
public class GenericRepository<T> where T : class
{
GPContext context;
IDbSet<T> dbSet;
public GenericRepository()
{
context = new GPContext();
dbSet = context.Set<T>();
}
public IEnumerable<T> getAll()
{
return dbSet.AsEnumerable();
}
public T GetById(int id)
{
return dbSet.Find(id);
}
public void Add(T t)
{
dbSet.Add(t);
context.SaveChanges();
}
public void Update(T t)
{
dbSet.Attach(t);
//on modifie la instance avant la inserer dans la bd
context.Entry(t).State = EntityState.Modified;
context.SaveChanges();
}
public void Delete(T t)
{
dbSet.Remove(t);
context.SaveChanges();
}
}
这是我的称呼Program.cs
-
GenericRepository<Product> productRepository = new GenericRepository<Product>();
Address adresse = new Address() { City = "Paris", StreetAdress = "Escalier 9" };
Category cat1 = new Category() { Name="cat1" };
Chemical p1 = new Chemical() { ProductId=26, DateProd = DateTime.Now , myAdress = adresse, Description = "CHemichal Product",
Price = 55,Name="PHARMA CHEMICH 1", Quantity = 100 , MyCategory = cat1};
Chemical p2 = new Chemical() {ProductId = 12 , DateProd = DateTime.Now , myAdress = adresse, Description = "CHemichal Update Product",
Price = 55,Name="PHARMA CHEMICH 1 Update", Quantity = 100 , MyCategory = cat1};
Product p3 = new Product() { ProductId = 11 };
productRepository.Add(p1);
productRepository.Update(p2);
productRepository.Delete(p3);
启动应用程序时发生以下错误-
System.InvalidOperationException:'发生了引用完整性约束冲突:关系一端的'Category.CategoryId'的属性值与另一端的'Product.categoryId'的属性值不匹配。 '
我在Delete
方法中也有此错误-
System.InvalidOperationException:'无法删除该对象,因为在ObjectStateManager中找不到该对象。
实体类别如下-
Product
:
public class Product : Concept
{
[Display(Name ="Date de prod")]
[DataType(DataType.Date)]
public DateTime DateProd { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Required(ErrorMessage ="Champ obligatoire")]
[StringLength(25)]
[MaxLength(50 , ErrorMessage ="taille max dans la base 50")]
public string Name { get; set; }
[DataType(DataType.Currency)]
public double Price { get; set; }
public int ProductId { get; set; }
[Range(0,int.MaxValue)]
public int Quantity { get; set; }
[DataType(DataType.ImageUrl) ,Display(Name ="Image")]
public string image { get; set; }
[ForeignKey("categoryId")]
public Category MyCategory { get; set; }
public int? categoryId { get; set; }
public List<Provider> Providers { get; set; }
public override void GetDetails()
{
Console.WriteLine("Product Name:, Price "+ Name + " " + Price);
}
public virtual void GetMyType()
{
Console.WriteLine("Uknown");
}
}
Chemical
:
public class Chemical : Product
{
// public string City { get; set; }
public string LastName { get; set; }
// public string StreetAddress { get; set; }
public Address myAdress { get; set; }
public override void GetMyType()
{
System.Console.WriteLine("CHEMICAL");
}
}
Biological
:
public class Biological : Product
{
public string Herbs { get; set; }
public override void GetMyType()
{
Console.WriteLine("Biological");
}
}
我真的是一个初学者,我不知道该做什么或更改。我已经阅读了同一主题的其他答案,但我不知道该怎么办。
首先,您要创建3个新实体,然后尝试添加,更新和删除它们。可以添加新实体,但不能更新或删除。考虑一下,它是您刚刚创建的对象,并且尚未在数据库中。您希望如何更新/删除它?要更新或删除实体,它必须是现有实体。
其次,你正在使用新创建(没有任何标识)Address
和Category
要更新的对象myAdress
和MyCategory
属性。这两个不是代表列值的原始属性,就像它们的其余部分一样,它们是具有外键约束的相关实体。如果您确实要更改/更新它们,则必须使用ID设置数据库中已经存在的一个。否则,它将违反外键约束(或参照完整性约束),这恰恰是第一条错误消息所说的。
请尝试以下操作,以进行更新-
// retrieve an existing address from database
Address newAddress = new GenericRepository<Address>().GetById(3);
// retrieve an existing category from database
Category newCategory = new GenericRepository<Category>().GetById(5);
// retrieve an existing product from database
Chemical p2 = new GenericRepository<Product>().GetById(12);
// update/change properties, but don't change the ProductId
p2.DateProd = DateTime.Now;
p2.myAdress = newAddress;
p2.Description = "CHemichal Update Product";
p2.Price = 55;
p2.Name="PHARMA CHEMICH 1 Update";
p2.Quantity = 100;
p2.MyCategory = newCategory;
// now call for update
productRepository.Update(p2);
而对于删除-
// retrieve an existing product from database
Product p3 = productRepository.GetById(11);
// now call for delete
productRepository.Delete(p3);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句