在模型类中实现NotMapped属性中的值实体框架代码首先使用linq [英] Get values in NotMapped property in model class Entity Framwork Code First using linq

查看:152
本文介绍了在模型类中实现NotMapped属性中的值实体框架代码首先使用linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的情况。我在VS 2010上使用EF 5代码,MVC 4。我正在使用工作单位和存储库模式作为我的项目。



我不知道这是否是可能与否。请注意。



我有一个表示数据库表的模型类。在模型类中,我有一个以[NotMapped]形式装饰的属性。我有一个Stored Proc返回数据,类似于模型类。但是,当我从SP中获取列表中的数据时,它不包含[NotMapped]列的值(SP返回[NotMapped]列的数据))。这可能在EF方面是逻辑上正确的。



我想知道的是,我们有没有办法为[NotMapped]列填充数据。我想实现,CRUD使用LINQ(不包括R - 读)。

解决方案

我建议创建一个单独的复杂类型存储过程结果。否则迟早会发现自己编写代码以区分来自DbSet的实体或存储过程。当来自存储过程的时候,它们不能在连接中使用,例如。或者检查未映射属性是否设置。



一个非常脏的方法可能是有两个不同的上下文。首先,使用代码,可以将不同的上下文与不同的映射关联到相同的类型,并且忽略列(如果使用流畅映射,而不是数据注释)。但是,如果您告诉EF不检查数据库模式,那么只有成功,所以使用迁移也被排除。我不会这样做!由于与上述相同的原因。我会讨厌有一个类型的属性,有时是有时是没有设置。


I have the below scenario. I am using EF 5 Code first, MVC 4 on VS 2010. I am using the Unit of Work and Repository pattern for my project.

I am not sure if this is possible or not. Kindly suggest.

I have a model class representing a database table. In the model class, I have a property that is decorated as [NotMapped]. I have a Stored Proc that returns data, similar to the model class. However, when I get the data in a List from the SP, it does not contain value for the [NotMapped] column (SP returns data for the [NotMapped] column though). This may be logically correct with respect to EF.

All I want to know is, do we have a way to get data populated for the [NotMapped] column. I want to achieve, CRUD using LINQ (excluding R - Read).

解决方案

I would recommend to create a separate complex type for the stored procedure results. Otherwise sooner or later you will find yourself writing code to distinguish between entities coming from the DbSet or from the stored procedure. When the come from the stored procedure they can't be used in joins, for example. Or checks whether or not the unmapped property is set.

A very dirty approach could be to have two different contexts. With code first it is possible to have different contexts with different mappings to the same types, with and without the column ignored (if you use fluent mapping, not with data annotations). But that only succeeds if you tell EF not to check the database schema, so using migrations is ruled out as well. I would not do it!! For the same reason as I mentioned above. I would hate to have a type with a property that sometimes is and sometimes isn't set.

这篇关于在模型类中实现NotMapped属性中的值实体框架代码首先使用linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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