Hi,
I have encountered Bug #1449 of https://github.com/aspnet/EntityFramework/issues/1449. According to the post, rowanmiller closed this Dec 8, 2015. However, I am still having this problem now. I am using Asp.Net Core 1.0. If someone has closed it, am I missing something or what?
Here is my situation: my Product class includes a list of Pictures (which is represented as image paths) within it
public class Product { [Key] public int ProductId { get; set; } public string ProductName { get; set; } ..... public List<FilePath> ImagePaths { get; set; } = new List<FilePath>(); } public class FilePath { [Key] public int FilePathId { get; set; } public int ProductId { get; set; }//foreign key public virtual Product product { get; set; }//reference navigation property public string Path { get; set; } }
when I add an ImagePaths to my Product, it's fine for the first time. However, when I add another ImagePaths, the first one get lost; when I add a 3rd one, the second one get lost; ... In short, the Product.List<FilePath> can keep only ONE copy. Here is my code:
Product currentProduct = ProductRepository.SelectByID(ProductId); FilePath fp = new FilePath { ProductId = ProductId, product = currentProduct, Path = FullFileName }; currentProduct.ImagePaths.Add(fp); ProductRepository.Update(currentProduct);
and I do have SaveChanges within my ProductRepository.Update() function:
public class ProductRepository : IProductRepository { private ApplicationDbContext _context = null; public ProductRepository(ApplicationDbContext db) { _context = db; } public void Update(Product product) { Product prodt = _context.Products.SingleOrDefault(c => c.ProductId == product.ProductId); if(prodt != null) { prodt.AdditionalShippingCharge = product.AdditionalShippingCharge; prodt.AdministratorNote = product.AdministratorNote; prodt.CategoryId = product.CategoryId; ....... _context.Entry(prodt).State = EntityState.Modified; _context.SaveChanges(); } } }
I am using SQL Server