什么是保持实体的原有属性,当他们不包括在MVC的编辑页面结合的最佳方式? [英] What is the best way to maintain an entity's original properties when they are not included in MVC binding from edit page?

查看:123
本文介绍了什么是保持实体的原有属性,当他们不包括在MVC的编辑页面结合的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC视图编辑模型对象。编辑页面,包括我的大多数对象的属性,但不是全部的 - 特别是它不包括CreatedOn和CreatedBy领域,因为这些都是在创建时设置(在我的服务层),并应在未来不会改变

I have an ASP.NET MVC view for editing a model object. The edit page includes most of the properties of my object but not all of them -- specifically it does not include CreatedOn and CreatedBy fields since those are set upon creation (in my service layer) and shouldn't change in the future.

除非我包括这些属性为隐藏字段,他们将不会被拾起绑定期间不可用时,我保存修改的对象在我的EF 4 DB语境。实际上,在保存原始值将由空值(或某些特定类型的默认设置)覆盖。

Unless I include these properties as hidden fields they will not be picked up during Binding and are unavailable when I save the modified object in my EF 4 DB Context. In actuality, upon save the original values would be overwritten by nulls (or some type-specific default).

我不希望作为隐藏字段删除这些,因为它是一种浪费字节,我不希望这些价值观暴露于潜在的操纵。

I don't want to drop these in as hidden fields because it is a waste of bytes and I don't want those values exposed to potential manipulation.

是否有一流的方式来处理这种情况?是否可以指定一个EF模型属性被忽略,除非明确设置?

Is there a "first class" way to handle this situation? Is it possible to specify a EF Model property is to be ignored unless explicitly set?

推荐答案

二者必选其一:

public bool SaveRecording(Recording recording)
{
    // Load only the DateTime property, not the full entity
    DateTime oldCreatedOn = db.Recordings
       .Where(r => r.Id == recording.Id)
       .Select(r => r.CreatedOn)
       .SingleOrDefault();

    recording.CreatedOn = oldCreatedOn;

    db.Entry(recording).State = EntityState.Modified;
    db.SaveChanges();

    return true;
}

修改的查询只从数据库加载的 CreatedOn 列,因此比装载全部的实体更便宜,速度更快,因为你。只需要在 CreatedOn 属性使用查找将不必要的开销:您加载的所有的性质,但只需要的他们中的一个的。另外装载的全部实体查找,然后取下使用它之后可以快捷键 AsNoTracking db.Recordings.AsNoTracking()的SingleOrDefault(R => r.Id == recording.Id); ,加载实体没有安装,所以你并不需要分离的实体。使用 AsNoTracking 使装载实体也更快。)

( The query only loads the CreatedOn column from the database and is therefore cheaper and faster than loading the full entity. Because you only need the CreatedOn property using Find would be unnecessary overhead: You load all properties but need only one of them. In addition loading the full entity with Find and then detach it afterwards could be shortcut by using AsNoTracking: db.Recordings.AsNoTracking().SingleOrDefault(r => r.Id == recording.Id); This loads the entity without attaching it, so you don't need to detach the entity. Using AsNoTracking makes loading the entity faster as well.)

编辑2

如果你想从你可以投射到一个匿名类型的数据库中加载多个属性:

If you want to load more than one property from the database you can project into an anonymous type:

public bool SaveRecording(Recording recording)
{
    // Load only the needed properties, not the full entity
    var originalData = db.Recordings
       .Where(r => r.Id == recording.Id)
       .Select(r => new
       {
           CreatedOn = r.CreatedOn,
           CreatedBy = r.CreatedBy
           // perhaps more fields...
       })
       .SingleOrDefault();

    recording.CreatedOn = originalData.CreatedOn;
    recording.CreatedBy = originalData.CreatedBy;
    // perhaps more...

    db.Entry(recording).State = EntityState.Modified;
    db.SaveChanges();

    return true;
}

(编辑2完)

(End of Edit 2)

或者

public bool SaveRecording(Recording recording)
{
    Recording oldVersion = db.Recordings.Find(recording.Id);

    recording.CreatedOn = oldVersion.CreatedOn;

    // flag only properties as modified which did really change
    db.Entry(oldVersion).CurrentValues.SetValues(recording);

    db.SaveChanges();

    return true;
}

修改使用 CurrentValues​​.SetValues​​ 标记仅属性修改这的确相比,在数据库中的原始状态已经改变时。你叫的SaveChanges EF将只发送属性标记为已修改在UPDATE语句到数据库。而设定的状态修改标记的所有的属性修改,不管他们是否真的变了还是不行。因为它包含了所有列的更新UPDATE语句将更加昂贵。)

( Using CurrentValues.SetValues flags only properties as Modified which indeed have been changed compared to the original state in the database. When you call SaveChanges EF will sent only the properties marked as modified in an UPDATE statement to the database. Whereas setting the state in Modified flags all properties as modified, no matter if they really changed or not. The UPDATE statement will be more expensive because it contains an update for all columns.)

这篇关于什么是保持实体的原有属性,当他们不包括在MVC的编辑页面结合的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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