简体   繁体   English

实体框架不会将数据库上下文中仍然存在的对象与从不同类引用的同一对象相关联

[英]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.

相关问题 C#实体框架6上下文对象数据库首先具有现有DbConnection - C# Entity Framework 6 Context Object Database First with existing DbConnection 实体框架不会从 MVC 中的实体数据模型(来自现有数据库)创建上下文和表类(.cs) - Entity Framework doesn't create Context and table classes(.cs) from Entity Data Model (from existing database) in MVC 实体框架4.3.1,DbContext使用现有引用对象添加对象 - Entity Framework 4.3.1 , DbContext Adding an object with existing referenced object 实体框架从上下文中删除对象,但不从数据库中删除 - Entity framework remove object from context, but not from database 有没有办法扩展实体框架对象,使其不映射到数据库? - Is there a way to extend an entity framework object so that it doesn't map to the database? 实体框架:IEntityChangeTracker的多个实例不能引用对象 - Entity Framework: Object can't be referenced by multiple instances of IEntityChangeTracker 将相同的对象附加到Entity Framework 6中的不同上下文 - Attach same object to different contexts in Entity Framework 6 实体框架不保存对象属性 - Entity framework doesn't save object property 关联来自不同数据库的表 - 实体框架 - Associate tables from different databases - Entity Framework 如何从现有引用对象以不同形式引用? - How to reference from an existing referenced object from different form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM