English 中文(简体)
实体框架: 多个实体不得援引意向书。
原标题:Entity Framework: An entity object cannot be referenced by multiple instances of IEntityChangeTracker

我有一个用户表,与警报表有着许多与人的关系。 在创建成员用户之后,我又在数据库中增加一些信息。

MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

if (createStatus == MembershipCreateStatus.Success)   {
User user = new MidTier.Models.User();
user.FullName = model.FullName;
if (Alerts.Count() > 0)
  {
    var userAlerts = SetAlert(Alerts); // creates an IEnumerable of Alerts (from a list of int )
    foreach (var alert in userAlerts)
         {
            user.Alerts.Add(alert);                  //add each alert to the user
          }                    
   }
   userRepository.Add(user); //throwing error
   userRepository.Save();
}

我错过(一个实体物体不能被多个IEntityChangeTracker.)称呼添加方法。 即便是关于SO,也有许多人提到这一错误,但在阅读了所有这些意见和建议之后,我就找到了解决办法,或者我发现这一错误的原因。

最佳回答

there are lots of references about this error on the net even here on SO, but after reading all those comments and suggestions, I a havent found a solution or the reason I am getting this error.

如果你真心实意地找你,就应该知道,错误告诉你,有些实体在标书上已经附属于其他情况。 由于你的代码样本大多不相关。 真正的重要法典载于您的方法——很可能是SetAlerts 用户交存。 添加。 如果这两种方法在内部使用,而不使用同一情况,那么这是你例外的原因。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...