OriginalValues不能用于已添加状态的实体 [英] OriginalValues cannot be used for entities in the Added state

查看:3064
本文介绍了OriginalValues不能用于已添加状态的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework构建一个Web应用程序,数据库是在启动时创建的,而我的种子方法将一些实体添加到数据库中没有任何问题。而且,实体的检索工作没有问题。



我的问题是,如果我尝试从我的UI层创建一个实体,我遇到错误 OriginalValues不能用于已添加状态中的实体。这个例外没有被抛到用户界面上,但是当我从这个问题中挖掘出来的时候我发现了这个例外。



我发生在我的

  public virtual TEntity Add(TEntity entity)
{
var entry = _context.Entry(entity);
entry.State = EntityState.Added;
_dbSet.Add(entity);

返回实体;
}

截图:





该实体非常小和映射:

  public abstract class EntityBase 
{
public int Id {get;组; }
}

public class AccessCode:EntityBase
{
public string Code {get;组; }
public int UsageCount {get;组; }
}

public class AccessCodeMapping:EntityTypeConfiguration< AccessCode>
{
public AccessCodeMapping()
{
// Table
ToTable(AccessCode);

//主键
HasKey(x => x.Id);

//属性
属性(accesscode => accesscode.Code).IsRequired()。HasMaxLength(256);
属性(accesscode => accesscode.UsageCount).IsRequired();
}
}

这是我如何创建测试访问代码演示目的

  var ac = new AccessCode {Code =321,UsageCount = 0}; 
_accessCodeService.Create(ac);
_unitOfWork.Save();
return View(Login);

任何人都可以弄清楚为什么会出现此错误?我迷路了。如果你想看到一些代码片段,请让我知道。

解决方案

我遇到这个问题一次,并通过检查提交到数据库的字段来解决它。事实证明,我无意中试图在设计为非空的列中插入一个空值。


I am using Entity Framework to build an web application, the database is created on startup and my seed method adds some entities to the database without any problem. Also the retrieve of the entities is working without problems.

My problem is that if I try to create an entity from my UI layer, I come across the error OriginalValues cannot be used for entities in the Added state. The exception is not thrown to the UI, but I found it when I digged around from the problem.

I happens in my:

public virtual TEntity Add(TEntity entity)
{
    var entry = _context.Entry(entity);
    entry.State = EntityState.Added;
    _dbSet.Add(entity);

    return entity;
}

Screenshot:

The entity is very small and the mappings:

public abstract class EntityBase
{
    public int Id { get; set; }
}

public class AccessCode : EntityBase
{
    public string Code { get; set; }
    public int UsageCount { get; set; }
}

public class AccessCodeMapping : EntityTypeConfiguration<AccessCode>
{
    public AccessCodeMapping()
    {
        // Table
        ToTable("AccessCode");

        // Primary key
        HasKey(x => x.Id);

        // Properties
        Property(accesscode => accesscode.Code).IsRequired().HasMaxLength(256);
        Property(accesscode => accesscode.UsageCount).IsRequired();
    }
}

And this is how I create a test access code for demo purpose

var ac = new AccessCode {Code = "321", UsageCount = 0};
_accessCodeService.Create(ac);
_unitOfWork.Save();
return View("Login");

Can anyone figure out why this error is occurring? I'm lost. :-)

P.s Let me know if there is some pieces of code you wish to see.

解决方案

I ran into this issue once, and resolved it by checking the fields that were being submitted to the database. It turns out that I inadvertently was attempting to insert a null value into a column that was designed as not null.

这篇关于OriginalValues不能用于已添加状态的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆