问题描述
我有一个问题,试图挽救这是一个外键父对象的实体。
I have a problem trying to save an entity which is a foreign key to a parent object..
我有一个amazonProduct实体。而这是对amazonProduct像这样一个虚拟的列表中AmazonCompetitivePrice实体:
i have an amazonProduct entity. And an AmazonCompetitivePrice entity which is a virtual list on the amazonProduct like so :
public class AmazonProduct
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public virtual int ASIN
public virtual IList<AmazonProductCompetitivePrice> amazonProductCompetitivePrices = new List<AmazonProductCompetitivePrice>();
}
所以我有一个AmazonProduct这是我从数据库中检索,然后添加一个新AmazonProductCompetitivePrice到amazonProduct。
So i have a AmazonProduct which i retrieve from the database, and then add a new AmazonProductCompetitivePrice to the amazonProduct.
但是当我试图挽救这个,我得到以下错误:
But when i try save this, I get the following error:
PRIMARY KEY约束'PK_dbo.AmazonProducts的相关规定。不能
插入重复的对象'dbo.AmazonProducts'.\r\\\
The声明
键已被终止
它看起来像它没有意识到,我是AmazonProduct已经在数据库中,它试图挽救一个新的,但主键已经存在!
It looks like its not realising that my AmazonProduct is already in the database, and its trying to save a new one but the primary key already exists!
我用流利的API映射,像这样的外键:
I used the fluent API to map the foreign key like so:
modelBuilder.Entity<AmazonProduct>()
.HasMany(pl => pl.AmazonProductCompetitivePrices)
.WithOptional(p => p.AmazonProduct)
.Map(c => c.MapKey("ASIN"));
任何人都知道什么是错呢?
Anyone know whats wrong with this?
在此先感谢
编辑:
对象检索:
object retrieval:
using (var uow = new UnitOfWorkInventory())
{
using (var amazRepo = new AmazonProductRepository(uow))
{
return amazRepo.FindByAsin(ASIN);
}
}
public AmazonProduct FindByAsin(string asin)
{
return context.AmazonProducts.Include(x => x.AmazonLowestOfferListings).Include(x => x.AmazonMyPrices).Include(x => x.AmazonProductCompetitivePrices).SingleOrDefault(x => x.ASIN == asin);
}
这让我的AmazonProduct ..然后保存:
That gets me the AmazonProduct.. then the save:
using (var uow = new UnitOfWorkInventory())
{
using (var amazonRepo = new AmazonProductCompetitivePriceRepository(uow))
{
amazonProductCompetitivePrice.AmazonProduct = amazonProduct;
amazonRepo.InsertOrUpdate(amazonProductCompetitivePrice);
}
uow.Commit();
}
public void InsertOrUpdate(AmazonProductCompetitivePrice amazonProductCompetitivePrice)
{
if (amazonProductCompetitivePrice.Id == default(int))
{
// New entity
context.AmazonProductCompetitivePrices.Add(amazonProductCompetitivePrice);
}
else
{
// Existing entity
context.Entry(amazonProductCompetitivePrice).State = EntityState.Modified;
}
}
这就是一切..并感谢您的帮助!
Thats everything.. and thanks for the help!!
推荐答案
这行:
amazonProductCompetitivePrice.AmazonProduct = amazonProduct;
补充 amazonProduct
实例上下文暗示。因为该实例与上下文的不同的实例检索,当前实例认为,这是新的 AmazonProduct
。你应该重视它,而不是,是这样的:
adds amazonProduct
instance to context implicitly. Because that instance was retrieved with different instance of context, current instance thinks, that it is new AmazonProduct
. You should attach it instead, something like this:
if (amazonProductCompetitivePrice.AmazonProduct != null && amazonProductCompetitivePrice.AmazonProduct.Id != 0)
{
context.Entry(amazonProductCompetitivePrice.AmazonProduct).State = EntityState.Unchanged;
}
另一种方法:你可以设置外键属性仅对 amazonProductCompetitivePrice
而不是导航属性。
这篇关于PRIMARY KEY约束'PK_dbo.AmazonProducts“的相关规定。不能在对象中插入重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!