我的应用程序中有这个错误,我不知道如何修复。
我有5张桌子:
Customer
Branch
PointOfSale
User
CustomerEndUser
具有以下关系:
Customer->Branch one-to-many
Branch->PointOfSale one-to-many
Customer->CustomerEndUser one-to-many
User->CustomerEndUser one-to-one
PointOfSale->CustomerEndUser one-to-many
我正在使用ADO.NET Entity Framework来完成数据库事务。
当我想添加一个新的CustomerEndUser时,我必须给它一个PointOfSale,一个用户和一个Customer。但每次我为CustomerEndUser调用Insert方法时,该方法抛出一个异常:“'idenTTsystemEntities.PointOfSaleSet'中的实体参与'FK_PointsOfSale_Branches'关系。找到0个相关的'分支'.1'分支'是预期的。该网站管理员将收到通知。请稍后再试。“,即使我向CustomerEndUser提供所需信息。
以下是我在Insert方法中使用的代码:
public static CustomerEndUser InsertIntUser(CustomerEndUser endUser)
{
idenTTsystemEntities context = new idenTTsystemEntities();
var mbr = Membership.Providers[INTMembershipProviderName];
MembershipCreateStatus userStatus;
if(mbr==null)
{
throw new NullReferenceException("Membership Provider is null");
}
MembershipUser mbrUser = mbr.CreateUser(endUser.Username, endUser.Password, endUser.Email, "no question",
"no answer", true, Guid.NewGuid(),
out userStatus);
if(userStatus==MembershipCreateStatus.Success)
{
ProfileBase profileBase = ProfileBase.Create(endUser.Username);
SettingsPropertyValueCollection profileProperties =
profileBase.Providers[INTProfileProviderName].GetPropertyValues(profileBase.Context,
ProfileBase.Properties);
//add the firstname, lastname and dateofbirth properties
profileProperties["FirstName"].PropertyValue = endUser.FirstName;
profileProperties["LastName"].PropertyValue = endUser.LastName;
var shortDate = new System.Globalization.DateTimeFormatInfo();
shortDate.ShortDatePattern = "dd.MM.yyyy";
profileProperties["DateOfBirth"].PropertyValue = Convert.ToDateTime(endUser.BirthDate, shortDate);
//get the id of the newly created end user
Guid newUserId = (Guid)mbrUser.ProviderUserKey;
//add the user to the CustomerEndUser
User newUser = context.UserSet.Where(u => u.UserId == newUserId).First();
endUser.User = newUser;
//get the customer to which the end user belongs to
Customer cust = context.CustomerSet.Where(c => c.CustomerId == CustomerId).First();
endUser.Customer = cust;
//get the pos selected in the insertform
Guid posId = endUser.PosId;
PointOfSale selectedPos = context.PointOfSaleSet.Include("Branch").Where(br => br.PointOfSaleId == posId).First();
endUser.PointOfSale = selectedPos;
endUser.EndUserId = Guid.NewGuid();
context.AddToCustomerEndUsers(endUser);
profileProperties["Language"].PropertyValue = "de-DE";
profileBase.Providers[INTProfileProviderName].SetPropertyValues(profileBase.Context, profileProperties);
profileBase.Save();
context.SaveChanges();
return endUser;
}
throw new MembershipCreateUserException(userStatus);
}
Google表示可以使用存储过程修复此问题,但如果可能的话,我想要一个不同的解决方案。谢谢!
干杯, 亚历克斯巴拉克
答案 0 :(得分:0)
你应该注意到PointsOfSale
应该与至少1 Branch
相关联。
对于Customer
,它也应该与Branch
相关。
所以解决方案就是当你加载PointsOfSale时,你也应该加载他的相关分支。(对客户而言)。 你知道怎么做,对吧?
答案 1 :(得分:0)
问题解决了,它与插入方法无关。每次我想创建一个新的EndUser时,在它的构造函数中我也创建了一个新的PointOfSale并将它附加到EndUser,因此EntityFramework认为我也插入了一个PointOfSale,并且期望一个Branch,即使我提供了一个。不要在构造函数中创建新的Object,而是使用EntityFramework创建的Reference.IsLoaded和Reference.EntityKey属性。
我希望这对那些有类似问题的人有足够的理解。
谢谢,
Alex Barac