无法确定“ OfferManagementModel.FK_ManagerSwipeRestriction_Restriction”关系的主要结尾。添加的多个实体可能具有相同的主键。
下面的代码正在保存一个存储库项目,我在下面的方法中遇到了上面的错误:
public static bool Ensure(OfferModel originalModel, OfferModel editedModel, Fishbowl.Common.DataAccess.OfferManagement.Entities.Offer existingOffer)
{
var repository = new BundlesForOfferRepository();
List<int> existingBundles = originalModel.ChosenBundleIds != null ? originalModel.ChosenBundleIds.ToList() : new List<int>();
List<int> wantedBundles = editedModel.ChosenBundleIds != null ? editedModel.ChosenBundleIds.ToList() : new List<int>();
bool madeChanges = false;
foreach (var oneExistingBundleId in existingBundles)
{
if (wantedBundles.Contains(oneExistingBundleId))
{
// This bundle is wanted and already mapped --- no change necessary.
}
else
{
// This bundle is not wanted but is already mapped --- drop it.
repository.Delete(originalModel.OfferId, oneExistingBundleId);
madeChanges = true;
}
}
foreach (int oneWantedBundleId in wantedBundles)
{
if (existingBundles.Contains(oneWantedBundleId))
{
// This bundle is wanted and already mapped --- no change necessary.
}
else
{
// This bundle is wanted but not yet mapped --- insert it.
repository.Add(originalModel.OfferId, oneWantedBundleId);
madeChanges = true;
}
}
if (madeChanges)
{
// Don't bother try/catch --- if there's an exception, let it be thrown
repository.SaveAllObjectChanges();
}
return madeChanges;
}
}
The runtime error is coming while doing repository.SaveAllObjectChanges() but originally it is coming on the "else" part where we are adding repository and calling the 'repository.Add(originalModel.OfferId, oneWantedBundleId);'
Below is the repository.add method:
public void Add(int offerId, int bundleId)
{
var bundleForOffer = this.Get(0);
bundleForOffer.OfferId = offerId;
bundleForOffer.BundleId = bundleId;
base.Add(bundleForOffer);
}
有人可以看看这个,并帮助我吗?如果您需要任何东西,请告诉我。