Framework中的特定字段上进行更新的最佳方法是什么

Framework中的特定字段上进行更新的最佳方法是什么

本文介绍了防止在Entity Framework中的特定字段上进行更新的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework为后端逻辑使用MVC编写Web应用程序。我的问题是我有一个具有某些字段的实体,这些字段在更新时永远不应更改。我不太确定解决这个问题的最佳方法是什么。我的应用程序中将要处理大量数据,因此我负担不起仅仅解决一个问题。

Im writing an web application with MVC using Entity Framework for my backend logic. My problem is that I have an entity that has certain fields that should never be changed on an update. I am not really sure what the best way to solve this problem would be. There is going to be a lot of data processed in my application, so I cant afford to just hack up a solution.

是否可以将字段定义为只读在POCO实体中?还是应该编写和实体框架扩展类来验证所有更新。可以在EF和实际数据库之间的映射文件中完成吗?

Is it possible to just define the fields as readonly in the POCO entities ? Or should I write and entity framework extension class that validates all updates. Could it be done in the mapping files between EF and the actual database?

我对EF还是比较陌生,所以希望你们中的一些人能给我一些帮助

I am relatively new with EF, so I hope some of you might be able to give me some pointers!

谢谢!

推荐答案

我会建议在视图中使用EF类。最好的选择是构造ViewModel类,并使用Automapper从EF类映射它们。

Well I would advice against ever using the EF classes in the View. You're best bet is to construct ViewModel classes and use Automapper to map them from the EF classes.

不过,当您更新数据库中的记录时,您可以控制哪个ViewModel中的字段用于更新EF类中的现有字段。

When you are updating records in the database though, you can control which fields in the ViewModel are used to update the existing fields in the EF class.

正常过程是:


  • 使用ID从数据库中获取现有对象的最新版本。

  • Use the Id to get the latest version of the existing object out of the database.

如果您使用的是乐观并发控制,请检查该对象是否自ViewModel以来未更新。创建(例如,检查时间戳)。

If you are using optimistic concurrency control then check that the object has not been updated since the ViewModel was created (so check timestamp for example).

使用ViewModel对象中的必填字段更新此对象。

Update this object with the required fields from your ViewModel object.

将更新后的对象持久化回到数据库中。

Persist the updated object back to the database.

进行更新以包括Automapper示例:

Update to include Automapper examples:

假设您的POCO是

public class MyObject
{
   public int Id {get;set;}
   public string Field1 {get;set;}
   public string Field2 {get;set;}
}

Field1是您不想更新的字段。

and Field1 is the field you don't want updating.

您应该声明具有相同属性的视图模型:

You should declare a view model with the same properties:

public class MyObjectModel
{
   public int Id {get;set;}
   public string Field1 {get;set;}
   public string Field2 {get;set;}
}

并在Controller的构造函数中在它们之间自动映射。

and Automap between them in the constructor of your Controller.

Mapper.CreateMap<MyObject, MyObjectModel>();

您可以根据需要(尽管我喜欢手动执行此操作,也可以通过其他方式自动映射:

you can if you wish (although I prefer to do this manually, automap the other way too:

Mapper.CreateMap<MyObjectModel, MyObject>().ForMember(dest=>dest.Field1, opt=>opt.Ignore());

将日期发送到网站时,您将使用:

When you are sending date to your website you would use:

 var myObjectModelInstance = Mapper.Map<MyObject, MyObjectModel>(myObjectInstance);

来创建viewModel。

to create the viewModel.

保存数据时,您可能想要类似的东西:

When saving the data, you'd probably want something like:

public JsonResult SaveMyObject(MyObjectModel myModel)
{
    var poco = Mapper.Map<MyObjectModel, MyObject>(myModel);
    if(myModel.Id == 0 )
    {
       //New object
       poco.Field1 = myModel.Field1 //set Field1 for new creates only

    }
}

尽管我可能会删除排除项o f上面的Field1并执行以下操作:

although I'd probably remove the exclusion of Field1 above and do something like:

public JsonResult SaveMyObject(MyObjectModel myModel)
{
   var poco;
   if(myModel.Id == 0)
   {
     poco = Mapper.Map<MyObjectModel, MyObject>(myModel);
   }
   else
   {
     poco = myDataLayer.GetMyObjectById(myModel.Id);
     poco.Field2 = myModel.Field2;
   }
   myDataLayer.SaveMyObject(poco);
}

请注意,我认为最佳实践永远不会让您从ViewModel自动映射,但要始终手动执行此操作,包括处理新商品。

note I believe that best-practise would have you never Automap FROM the ViewModel, but to always do this manually, including for new items.

这篇关于防止在Entity Framework中的特定字段上进行更新的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:14