当更新标记实体的日期时间属性不变 [英] When updating Marking entity datetime property unchanged

查看:93
本文介绍了当更新标记实体的日期时间属性不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期时间类型的属性我的域模型这个实体服务entrydate和updatedon。

当用户在编辑视图中所做的任何更改,并提交表格后,我想postedback /修改的对象entrydate财产被标记为不变所以执行更新时entrydate不能被覆盖。

 公共类服务
{
    公众诠释服务ID
    {
        得到;
        组;    }
    [必需(的ErrorMessage =请输入名称)]
    公共字符串名称
    {
        得到;
        组;
    }    [必需(的ErrorMessage =请输入持续时间为服务)]
    公众持续时间短
    {
        得到;
        组;
    }
    [数据类型(DataType.Date)
    公众的DateTime EntryDate
    {
        得到;
        组;
    }    [数据类型(DataType.Date)
    公众的DateTime UpdatedOn
    {
        得到;
        组;
    }    公共小数成本
    {
        得到;组;    }
}

这是坚持转变为DB信息库的方法如下:

 公共无效InsertOrUpdate(Service服务)
    {
        如果(service.ServiceID ==默认值(INT)){
            //新建实体
            context.Services.Add(服务);
        }其他{
            //现有实体            context.Entry(服务).STATE = EntityState.Modified;
        }
    }


解决方案

您可以从数据库中重新加载原始实体:

 其他{
    //现有实体
    VAR serviceInDb = context.Services.Find(service.ServiceID);
    service.EntryDate = serviceInDb.EntryDate;
    context.Entry(serviceInDb).CurrentValues​​.SetValues​​(服务);
}

当你调用的SaveChanges 后仅适用于已经真正改变将被发送到数据库属性的UPDATE语句(也有其他不变性质好处)。

或者只是重新加载 EntryDate

 其他{
    //现有实体
    VAR entryDateInDb = context.Services
        。选择(S => s.EntryDate)
        。单(S => s.ServiceID == service.ServiceID);
    service.EntryDate = entryDateInDb;
    context.Entry(服务).STATE = EntityState.Modified;
}

另一个工作但丑陋的做法是这样的:

  context.Services.Attach(服务); //感谢user202448,看评论context.Entry(服务).Property(S = GT; s.Name).IsModified = TRUE;
context.Entry(服务).Property(S = GT; s.Duration).IsModified = TRUE;
context.Entry(服务).Property(S = GT; s.UpdatedOn).IsModified = TRUE;
context.Entry(服务).Property(S = GT; s.Cost).IsModified = TRUE;

所以,不要在 EntryDate 属性设置为修改,但所有其他属性逐个。

该进入脑海的方法...

  context.Entry(服务).STATE = EntityState.Modified;
context.Entry(服务).Property(S = GT; s.EntryDate).IsModified = FALSE;

...不幸的是不起作用,因为设置回属性不修饰它已经被标记为修改不支持,将抛出异常。

I have this entity service in my domain model with two datetime type properties entrydate and updatedon.

When user in edit view make any changes and submit form back I want entrydate property of the postedback/modified object to be marked as unchanged so entrydate can't be overwritten when performing updates.

public class Service
{
    public int ServiceID
    {
        get;
        set;

    }
    [Required(ErrorMessage="Please enter Name")]
    public string Name
    {
        get;
        set;
    }

    [Required(ErrorMessage="Please enter the duration for the service")]
    public short Duration
    {
        get;
        set;
    }


    [DataType(DataType.Date)]
    public DateTime EntryDate
    {
        get;
        set;
    }

    [DataType(DataType.Date)]
    public DateTime UpdatedOn
    {
        get;
        set;
    }



    public decimal Cost
    {
        get; set;

    }
}

Repository method that is persisting changes into db is as follows:

 public void InsertOrUpdate(Service service)
    {
        if (service.ServiceID == default(int)) {
            // New entity
            context.Services.Add(service);
        } else {
            // Existing entity

            context.Entry(service).State = EntityState.Modified;
        }
    }

解决方案

You can reload the original entity from the database:

else {
    // Existing entity
    var serviceInDb = context.Services.Find(service.ServiceID);
    service.EntryDate = serviceInDb.EntryDate;
    context.Entry(serviceInDb).CurrentValues.SetValues(service);
}

When you call SaveChanges later an UPDATE statement only for properties which have really changed will be sent to the database (has also benefits for other unchanged properties).

Or just reload the EntryDate:

else {
    // Existing entity
    var entryDateInDb = context.Services
        .Select(s => s.EntryDate)
        .Single(s => s.ServiceID == service.ServiceID);
    service.EntryDate = entryDateInDb;
    context.Entry(service).State = EntityState.Modified;
}

Another working but ugly approach is this:

context.Services.Attach(service); // thanks to user202448, see comments

context.Entry(service).Property(s => s.Name).IsModified = true;
context.Entry(service).Property(s => s.Duration).IsModified = true;
context.Entry(service).Property(s => s.UpdatedOn).IsModified = true;
context.Entry(service).Property(s => s.Cost).IsModified = true;

So, don't set the EntryDate property to modified but all the other properties one by one.

The approach which comes into mind ...

context.Entry(service).State = EntityState.Modified;
context.Entry(service).Property(s => s.EntryDate).IsModified = false;

... unfortunately doesn't work because setting back a property to not-modified which is already marked as Modified is not supported and will throw an exception.

这篇关于当更新标记实体的日期时间属性不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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