防止在实体框架中的相关表实体上添加新记录

Prevent Adding New Record on Related Table Entity in Entity Framework(防止在实体框架中的相关表实体上添加新记录)

本文介绍了防止在实体框架中的相关表实体上添加新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的实体上添加新记录.它工作正常,问题是相关实体也在添加新记录.有没有办法阻止相关实体或二级实体也插入新记录?

I am trying to add new record on my entity. It works fine, the problem is, the related entities are adding new records as well. Is there a way to stop related or 2nd level entities to be inserting new records as well?

这是我的示例实体类:

public Tracking()
    {
        public string Details { get; set; }

        //Other properties here..

        [Required]
        public virtual Employee { get; set; }
    }

基本上我只是获取现有的员工记录,然后在我的财产上声明它,然后添加跟踪记录:

Basically I am just getting the existing Employee record then declare it on my property, then add the the Tracking record:

Employee emp = _dbContext.EmployeeRepo.GetEmployeeByID(1001);

Tracking track = new Tracking()
{
   Details = "My details here",
   Employee = emp 
}

_dbContext.TrackingRepo.Add(track);
_dbContext.SaveChanges();

这段代码工作正常,问题是,另一个新的员工记录被插入到我的表 Employee 中.这不是我想要的.我只想在现有员工记录中添加新的 Tracking 记录.

This code works fine, the problem is that, another new Employee Record is inserted on my table Employee. Which is not what I want. I just want to add a new record of Tracking with the existing employee record.

那么有没有办法做到这一点,或者我的 Entity Framework 缺少配置或代码?

So is there a way to do this or I am missing a configuration or code with my Entity Framework?

推荐答案

您需要将员工设置/标记为UnchangedAttach实体

You need to set/mark the employee as Unchanged or Attach the entity

如果您有一个您知道数据库中已经存在但当前没有被上下文跟踪的实体,那么您可以使用 DbSet 上的 Attach 方法告诉上下文跟踪该实体.实体在上下文中将处于 Unchanged 状态

If you have an entity that you know already exists in the database but which is not currently being tracked by the context then you can tell the context to track the entity using the Attach method on DbSet. The entity will be in the Unchanged state in the context

context.Employees.Attach(emp); 

请注意,如果调用 SaveChanges 而不对附加实体进行任何其他操作,则不会对数据库进行任何更改.这是因为实体处于 Unchanged 状态.

Note that no changes will be made to the database if SaveChanges is called without doing any other manipulation of the attached entity. This is because the entity is in the Unchanged state.

将现有实体附加到上下文的另一种方法是将其状态更改为未更改.

Another way to attach an existing entity to the context is to change its state to Unchanged.

context.Entry(emp).State = EntityState.Unchanged;

<小时>

EntityState 枚举

未更改 对象自附加到上下文或自上次使用 SaveChanges 方法后未修改调用.

Unchanged The object has not been modified since it was attached to the context or since the last time that the SaveChanges method was called.

<小时>

实体框架添加和附加以及实体状态

实体状态和 SaveChanges

实体可以处于 EntityState 枚举定义的五种状态之一.这些状态是:

An entity can be in one of five states as defined by the EntityState enumeration. These states are:

  • 添加:实体正在被上下文跟踪,但在数据库中尚不存在
  • 未更改:实体正在被上下文跟踪并存在于数据库中,并且其属性值与值没有改变在数据库中
  • 已修改:实体正在被上下文跟踪并存在于数据库中,并且其部分或全部属性值已被修改修改
  • 已删除:实体正在被上下文跟踪并存在于数据库中,但已被标记为从数据库中删除下次调用 SaveChanges 时
  • 已分离:上下文未跟踪实体

SaveChanges 对不同状态的实体做不同的事情:

SaveChanges does different things for entities in different states:

  • SaveChanges 不会触及未更改的实体.对于处于未更改状态的实体,更新不会发送到数据库.
  • 添加的实体被插入到数据库中,然后在 SaveChanges 返回时变为 Unchanged.
  • 修改后的实体在数据库中更新,然后在 SaveChanges 返回时变为未更改.
  • 删除的实体会从数据库中删除,然后从上下文中分离出来.

这篇关于防止在实体框架中的相关表实体上添加新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:防止在实体框架中的相关表实体上添加新记录

基础教程推荐