EF:将数据从不同的数据源映射到实体属性 [英] EF : Mapping data from different datasource to entity properties

查看:80
本文介绍了EF:将数据从不同的数据源映射到实体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我已经使用EF一段时间了,我发现它非常好用。

Hello! I have been using EF for some time and I find it very nice and useful.

这些天我正在研究如何使用EF实体作为业务对象。这是我的场景:

These days I am looking on how to use EF entities as business objects. Here is my scenario:

我有一个数据库模式,它映射到我的EF模型,一切正常。我仅将数据库数据存储用于历史信息存储。我有一个正在服务器中执行的进程,我想将这个
进程中的一些信息添加到从数据存储返回的实体对象中。我不想像存根那样向数据库模式添加额外的列,而是向生成的实体添加一些客户端业务逻辑。

I have a database schema, which is mapped to my EF model and everything works fine. I am using my database data store only for historical information store only. I have a process that is executing in the server and I want to add some information from this process to the entity object, returned from the data store. I don't want to add additional columns to the database schema just as a stub, but rather add some client side business logic to the generated entity.

目前我添加了一个部分类和它的一些属性,但我不能将它与LINQ to Entities一起使用。我试过POCO也没有成功。我需要能够在/ orderby表达式中包含我的自定义属性。模型定义的函数
似乎无法解决我的问题。

Currently I have added a partial class and some properties to it, but I can't use it with LINQ to Entities. I tried with POCO and no success there too. I need to be able to include my custom properties in where / orderby expressions. Model Defined Functions can't seem to solve my case.

你能告诉我这是否可以与EF一起使用或者我的构思是否有问题。

Can you tell me if this can be don with EF or if there is something wrong with my conception.

推荐答案

如果我理解你的问题,你想要的是能够拥有一个持久保存到数据库的属性的对象和一些属性不会保留,而是从其他来源填写。 是吗? 
技巧是你要编写涉及数据库中的属性和其他属性的查询,对吗?

If I understand your question, what you want is the ability to have an object which has some properties persisted to the database and some properties that are not persisted but instead filled in from some other source.  Is that right?  And the trick is that you want to write queries that involve both properties in the database and the other properties, correct?

可以编写涉及两种属性的查询属性,但您必须在运行在数据库上的查询部分和在客户端本地运行的部分之间创建显式边界。 你为服务器编写部分,
然后调用AsEnumerable()并编写本地运行的部分。

It is possible to write queries that involve both kinds of properties, but you have to create an explicit boundary between the part of the query which runs on the database and the part which runs locally on the client.  You write the part for the server, then call AsEnumerable() and write the part that runs locally.

但我不明白的是,你是如何添加的结果从服务器返回时客户端上的数据。 你能给我一些简单的示例代码来说明你的想法吗?

The thing I don't understand, though, is how you add data on the client when results are coming back from the server.  Can you give me a short bit of example code which shows what you have in mind?

- Danny

- Danny


这篇关于EF:将数据从不同的数据源映射到实体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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