[英]Entity Framework doesn't associate a still existing object in the database context with the same object referenced from a different class
I'm trying to add a message
to my database 我正在尝试向我的数据库添加一条message
Message.cs: Message.cs:
public class Message
{
public int Id { get; set; }
public int SenderId { get; set; }
public virtual User Sender { get; set; }
public virtual Collection<Recipient> Recipients { get; set; }
public MessageTrigger Trigger { get; set; }
public string Body { get; set; }
public DateTime CreatedTs { get; set; }
}
I have also a table named Recipients
我还有一个名为“ Recipients
的表
Recipient.cs: Recipient.cs:
public class Recipient
{
public int Id { get; set; }
public int MessageId { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public DateTime? DeliviredOn { get; set; }
}
As you see both tables have their foreign key set to the Id of an User 如您所见,两个表的外键都设置为用户ID
User.cs: User.cs:
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Now while adding a new Message to the database 现在,在向数据库添加新消息时
public void Add(Message message)
{
using (var ctx = new ShowcaseContext())
{
foreach (var reci in message.Recipients)
{
ctx.Users.Attach(reci.User);
}
ctx.Messages.Add(message);
ctx.SaveChanges();
}
}
I have to attach all the user to the context, because if not EF is adding the User again to the database. 我必须将所有用户附加到上下文,因为如果不是这样,EF会将用户再次添加到数据库。 This worked all fine while Message
doesn't had a Sender
. 当Message
没有Sender
一切正常。 Now after adding the Sender and SenderId
properties to the Message
class, EF adds the user
again to the user table. 现在,在将Sender and SenderId
属性添加到Message
类之后,EF将user
再次添加到用户表中。 So I tried something like that: 所以我尝试了这样的事情:
public void Add(Message message)
{
using (var ctx = new ShowcaseContext())
{
foreach (var reci in message.Recipients)
{
ctx.Users.Attach(reci.User);
}
ctx.Users.Attach(message.Sender); // Here the error is thrown
ctx.Messages.Add(message);
ctx.SaveChanges();
}
}
But then this Error occures: 但是然后发生此错误:
Error attaching an entity of type Entities.User, because another entity of the same type already has an identical primary key value 附加Entities.User类型的实体时出错,因为相同类型的另一个实体已经具有相同的主键值
So the user of sender
is already in the database context, but the context doesn't seem to associate this user with the user of the sender
. 因此, sender
的用户已经在数据库上下文中,但是上下文似乎并未将此用户与sender
的用户相关联。
And because of that the userobject of sender
is added to the database as an duplicate. 因此, sender
对象将作为副本添加到数据库中。
Is there someone out there who knows a solution for this problem? 那里有人知道解决此问题的方法吗?
I just want to add a message
to the database without EF adding an already existing user again to the user table. 我只想向数据库添加一条message
,而无需EF将已经存在的用户再次添加到用户表中。
I have solved the problem with this: 我已经解决了这个问题:
public void Add(Message message)
{
using (var ctx = new ShowcaseContext())
{
foreach (var reci in message.Recipients)
{
ctx.Users.Attach(reci.User);
}
ctx.Messages.Add(message);
ctx.Entry(message.Sender).State = EntityState.Detached;
ctx.SaveChanges();
}
}
After adding the message to the context, I explicitly detach the sender
user object from the context. 在将消息添加到上下文之后,我明确地将sender
用户对象与上下文分离。
I guess it is not the best solution, but the only one I can think of which works. 我想这不是最好的解决方案,但我认为唯一可行的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.