我的实体是:
public class Customer { ... public virtual ICollection<ShoppingCartItem> ShoppingCartItems { get; set; } ... } public class ShoppingCartItem { public string CustomerId { get; set; } public int ProductId { get; set; } public virtual Customer { get; set; } public virtual Product{ get; set; } ... }add方法是:
public async Task AddAsync(TEntity entity) { await Task.Factory.StartNew(() => this.entities.Add(entity)); }我要添加的实体是:
ShoppingCartItem() { CustomerId = "xxxxx", ProductId = 1, Customer = null, Product = null }当我调用SaveChanges()时,EF正在尝试为ShoppingCartItem插入两个相同的记录。 ShoppingCartItem只创建一次并添加到上下文中。 什么可能是错的想法?
编辑:
这就是我调用AddSync方法的方法:
public async Task AddNewCartItem(ShoppingCartItem shopingCartItem) { await this.ShoppingCartItemRepository.AddAsync(shopingCartItem); await this.SmartStoreWorkData.CompleteAsync(); }My Entities are:
public class Customer { ... public virtual ICollection<ShoppingCartItem> ShoppingCartItems { get; set; } ... } public class ShoppingCartItem { public string CustomerId { get; set; } public int ProductId { get; set; } public virtual Customer { get; set; } public virtual Product{ get; set; } ... }The add method is:
public async Task AddAsync(TEntity entity) { await Task.Factory.StartNew(() => this.entities.Add(entity)); }The entity that I am adding is:
ShoppingCartItem() { CustomerId = "xxxxx", ProductId = 1, Customer = null, Product = null }When I call SaveChanges() the EF is trying to insert two identical records for ShoppingCartItem. The ShoppingCartItem is created and added to the context only once. Any ideas what possibly could be wrong?
EDIT:
This is how I call the AddSync method:
public async Task AddNewCartItem(ShoppingCartItem shopingCartItem) { await this.ShoppingCartItemRepository.AddAsync(shopingCartItem); await this.SmartStoreWorkData.CompleteAsync(); }最满意答案
更新:我做了以下事情:
克隆您的repo链接: SmartStoreNETCore 使用新的EF工具迁移到.NET Core 1.1(preview4) 添加了下面的EF ModelBuilder中指定的配置 应用迁移和更新的数据库命令:
dotnet ef --startup-project ../SmartStoreNetCore.Web/ migrations add ChangeShoppingCartItemKey dotnet ef --startup-project ../SmartStoreNetCore.Web/ database update删除了_Layout.cshtml的以下重复标记
<script src="~/js/site.js" asp-append-version="true"></script>
启动网站, 一切按预期工作,没有重复的购物车项目,数量按预期更新site.js包含“ 添加到购物车”功能的单击事件处理程序
综上所述
public async Task AddNewCartItem(ShoppingCartItem shopingCartItem) { await this.ShoppingCartItemRepository.AddAsync(shopingCartItem); await this.SmartStoreWorkData.CompleteAsync(); }我可以完全确认在删除对site.js的重复引用之前,已调用以下方法两次 :
对我来说,为什么在调试前没有发现这个问题,这是一种谜
EF ModelBuilder
您的配置应如下所示:
builder.Entity<ShoppingCartItem>().HasKey(x => x.Id); // Notice this! builder.Entity<ShoppingCartItem>().Property(x => x.Id).ValueGeneratedOnAdd(); // Also this! builder.Entity<ShoppingCartItem>().HasOne(s => s.Customer).WithMany(b => b.ShoppingCartItems).OnDelete(DeleteBehavior.Restrict); builder.Entity<ShoppingCartItem>().HasOne(s => s.Product).WithMany().OnDelete(DeleteBehavior.Restrict);拥有自动生成的值不会将其定义为主键
UPDATE: I did the following:
Cloned your repo link: SmartStoreNETCore Migrated to .NET Core 1.1 with the new EF tooling (preview4) Added the configuration as specified in EF ModelBuilder below Applied Migration and updated databaseCommands:
dotnet ef --startup-project ../SmartStoreNetCore.Web/ migrations add ChangeShoppingCartItemKey dotnet ef --startup-project ../SmartStoreNetCore.Web/ database updateRemoved the following duplicate tag in _Layout.cshtml
<script src="~/js/site.js" asp-append-version="true"></script>
Started the site and everything worked as expected, no duplicate shopping cart items and the quantity is updated as expectedsite.js contains the click event handler for the Add To Cart functionality
In summary
public async Task AddNewCartItem(ShoppingCartItem shopingCartItem) { await this.ShoppingCartItemRepository.AddAsync(shopingCartItem); await this.SmartStoreWorkData.CompleteAsync(); }I can fully confirm the following method was being called twice before removing the duplicate reference to site.js:
It is a mistery to me why you didn't catch this before via debugging
EF ModelBuilder
Your configuration should look like:
builder.Entity<ShoppingCartItem>().HasKey(x => x.Id); // Notice this! builder.Entity<ShoppingCartItem>().Property(x => x.Id).ValueGeneratedOnAdd(); // Also this! builder.Entity<ShoppingCartItem>().HasOne(s => s.Customer).WithMany(b => b.ShoppingCartItems).OnDelete(DeleteBehavior.Restrict); builder.Entity<ShoppingCartItem>().HasOne(s => s.Product).WithMany().OnDelete(DeleteBehavior.Restrict);Having a value generated automatically does not define it as the primary key
更多推荐
ShoppingCartItem,public,The,entity,电脑培训,计算机培训,IT培训"/> <meta name=
发布评论