我目前正在使用Entity Framwork 6开发一个新的ASP.NET MVC应用程序。 我有两个一起工作的课程:帐户和帐户添加。 帐户代表基本用户帐户,AccountAddition包含例如联系人,通知或上次登录时间的ICollections。
现在我只想通过curAcc.Addition.Contacts.Add(correspondingAccount)
向AccountAdditions ICollection Contacts属性添加一个新帐户,其中correspondingAccount
是要添加的帐户对象(两个帐户都在数据库中提供)。
问题在于,当我将correspondingAccount
添加到当前帐户的联系人时,引用correspondingAccount.Addition
会突然指向当前帐户的AccountAddition
。
问题部分:
...
Account curAcc = context.Accounts.FirstOrDefault(p => p.Alias == currentAlias);
if(ModelState.IsValid)
{
var correspondingAccount = context.Accounts.FirstOrDefault(p => p.Alias == addAlias);
if(correspondingAccount != null)
{
curAcc.Addition.Contacts.Add(correspondingAccount);
context.SaveChanges();
...
代码第一类:
public class Account
{
public Account()
{
IsEnabled = true;
}
[Key]
public int ID { get; set; }
public string Alias { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
public byte[] Salt { get; set; }
[ForeignKey("Config")]
public int ConfigID { get; set; }
public virtual CryptoConfig Config { get; set; }
[ForeignKey("Addition")]
public int AdditionID { get; set; }
public virtual AccountAddition Addition { get; set; }
public virtual ICollection<AccountRoleLink> Roles { get; set; }
public bool IsEnabled { get; set; }
}
public class AccountAddition
{
public AccountAddition()
{
CreatedOn = DateTime.Now;
Contacts = new List<Account>();
Notifications = new List<Notification>();
}
[Key]
public int ID { get; set; }
public virtual ICollection<Account> Contacts { get; set; }
public virtual ICollection<Notification> Notifications { get; set; }
public string ContactEmail { get; set; }
public Nullable<DateTime> LastLogin { get; set; }
public Nullable<DateTime> LastFailedLogin { get; set; }
public Nullable<DateTime> CreatedOn { get; set; }
}
更多示例:
Account curAcc = // Find current account (current context)
Account correspondingAcc = // Get account to add (same context)
curAcc.Addition is Addition with id 1
correspondingAcc.Addition is Addition with id 2
// correspondingAcc gets added via curAcc.Addition.Contacts.add(cor...)
// context saves changes
curAcc.Addition is Addition with id 2 afterwards
correspondingAcc.Addition is Addition with id 2 afterwards
我尝试在Google教程中使用Google搜索和搜索,但没有找到解决方案。 有什么问题?
感谢您的时间
更新:
好吧显然Ody提供的解决方案没有按预期工作。 我尝试从Notification和Contact集合中删除virtual关键字。 剪断的外观现在是这样的:
Account curAcc = GetCurrentAccount();
if(ModelState.IsValid)
{
var correspondingAccount = context.Accounts.FirstOrDefault(p => p.Alias == addAlias);
if(correspondingAccount != null)
{
curAcc.Addition.Contacts.Add(correspondingAccount);
var existingCorrespondingAccount = curAcc.Addition
.Contacts.Where(a => a.Alias == addAlias)
.FirstOrDefault();
if (existingCorrespondingAccount != null)
{
context.Accounts.Add(curAcc);
}
context.SaveChanges();
在调用Add方法之后,context.Accounts中的帐户对象指向错误的AccountAddition。当我想将帐户f2(AdditionId 2)添加到帐户f1(AdditionId 1)时,f2的additionId指向1。
当我调用SaveChanges时,抛出System.Data.Entity.Infrastructure.DbUpdateException。
{&#34;保存不公开其关系的外键属性的实体时发生错误。 EntityEntries属性将返回null,因为无法将单个实体标识为异常源。通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常。有关详细信息,请参阅InnerException。&#34;}
内部异常是
{&#34;存储更新,插入或删除语句影响了意外的行数(0)。自实体加载后,实体可能已被修改或删除。有关理解和处理乐观并发异常的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=472540。&#34;}
我按照链接尝试使用OptimisticConcurrencyException和DbUpdateConcurrencyException捕获它,但是ex是DbUpdateException,它没有被它们捕获。
这令人沮丧:(
答案 0 :(得分:0)
问题在于,每当您将实体添加到相关属性集合并调用SaveChanges()
时,它都不会检查该ID是否已存在。无论如何它创造了一个新的。这就是EF的工作方式。
要解决您的问题,您应该先检查帐户是否已存在于acc.Contacts中,然后再手动添加ID。
像这样的东西
Account curAcc = context.Accounts.FirstOrDefault(p => p.Alias == currentAlias);
var correspondingAccount = context.Accounts.FirstOrDefault(p => p.Alias == addAlias);
if (correspondingAccount != null)
{
curAcc.Addition.Contacts.Add(correspondingAccount);
var existingCorrespondingAccount = curAcc.Addition
.Contacts.Where(a => a.Alias == addAlias)
.FirstOrDefault();
if(existingCorrespondingAccount != null)
{
context.Accounts.Add(new Account
{
AdditionID = curAcc.AdditionID,
Alias = curAcc.Alias,
ConfigID = curAcc.ConfigID,
IsEnabled = curAcc.IsEnabled,
Password = curAcc.Password,
Salt = curAcc.Salt
});
}
context.SaveChanges();
}