简体   繁体   English

向实体框架模型添加属性

[英]Add property to entity framework model

I am using Entity Framework in my dataaccess layer, i also use this as my object model. 我在数据访问层中使用实体框架,也将其用作对象模型。

psudo object model: 伪对象模型:

public class Item{
    public string ID{get;set;}
    public string Name{get;set;}
    public Xml Data{get;set;}
}

The Data property is the key here. 数据属性是这里的关键。 i would like to be able to do this: 我希望能够做到这一点:

Psudo: Psudo:

var item = Items.Get(id: 1);
var xmlValue = item["key"];

I would like to be able to extend the EF generated class Item with a new property where i use some logic to get a value by key from the xml. 我希望能够用新属性扩展EF生成的类Item,在该属性中,我使用一些逻辑从xml通过键获取值。

My problem is: 我的问题是:

  1. I find EF data model not very easy to work with and when i want to update the Item table, i cannot be sure my extended logic will stand. 我发现EF数据模型不是很容易使用,并且当我想更新Item表时,我不确定我的扩展逻辑是否会成立。
  2. I dont think i want to add an extracted model and do a convert the EF Item to My.Item every time. 我不认为我想添加提取的模型并每次将EF项目转换为My.Item。 This will be a heavy opeartion if the table gets big. 如果桌子变大,这将是一个沉重的负担。

What is a good way to extend the EF object model? 扩展EF对象模型的好方法是什么?

that i dont 我不

You could just use a partial class and define the extra fields / properties. 您可以只使用局部类并定义额外的字段/属性。

Converting from EF Item to your item is trivial if the fields are the same, you could just use Automapper 如果字段相同,则从EF项转换为项是微不足道的,您可以使用Automapper

i'm leaning towards the partial class solution though. 我倾向于偏类解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM