动态加载实体框架中的SQL表 [英] Dynamically loading SQL tables in Entity Framework

查看:115
本文介绍了动态加载实体框架中的SQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态地访问希望使用实体框架的一些SQL表。下面是一些伪code:

I need to dynamically access some SQL tables hopefully using the Entity Framework. Here's some pseudo code:

VAR帐户= DB.Accounts.SingleOrDefault(X => x.ID == 12345);

这将返回我一个交代对象,这包含了某些字段名为preFIX,广告系列ID以及有关账户的详细信息都存储在preFIX_CAMPAIGNID_MAIN的命名约定单独的SQL表。

which will return me an Account object and this contains some fields called "PREFIX", "CAMPAIGN ID" and further information about the accounts are stored in separate SQL tables with the naming convention of PREFIX_CAMPAIGNID_MAIN.

这些表都具有相同的领域,所以我想创建一个不被任何映射和一个新的实体,那么动态加载它,像这样:

The tables all have the same fields so I was thinking of creating a new Entity that isn't mapped anywhere and then dynamically loading it, like so:

VAR STA01_MAIN =新我的帐户(); //我的非映射实体

DB.LoadTable('STA01_MAIN')LoadInto(STA01_MAIN);

我现在可以获取有关STA01_MAIN帐户什么: STA01_MAIN.AccountId

I can now get anything about the STA01_MAIN account: STA01_MAIN.AccountId.

我的问题是:我怎么访问使用实体框架这些表

So my question is: how do I access these tables using the Entity Framework?

推荐答案

我不认为EF具有可装入和LoadInto方法,但 ObjectOntext.ExecuteStoreQuery 可能是什么你要找的:

I don't think EF has a LoadTable and LoadInto method, but ObjectOntext.ExecuteStoreQuery might be what you're looking for:

http://msdn.microsoft.com/en-us/library/ dd487208.aspx

这应该让你对数据库执行任意的查询,然后将结果到您指定的(即使它不是以其他方式映射到EF)任意类型映射。

This should let you execute an arbitrary query against your database, and then map the results to an arbitrary type that you specify (even if it's not otherwise mapped in EF).

不言而喻,你将负责放在一起使供给必要的列映射到目标类型的查询,并且还调节时,所述查询这种类型的变化。

It goes without saying that you would be responsible for putting together a query that supplied the necessary columns for mapping into the destination type, and also adjusting said query when this type changes.

下面是关于它的用法进一步讨论

Here's some further discussion concerning its usage

<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/44cf5582-63f8-4f81-8029-7b43469c028d/\" rel=\"nofollow\">http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/44cf5582-63f8-4f81-8029-7b43469c028d/

你有没有考虑所有这些表(用同一列)映射到EF继承关系,然后询问他们作为

Have you considered mapping all of these tables (with the identical columns) into an inheritance relationship in EF, and then querying them as

db.BaseTypes.OfType&LT; SpecificType方式&gt;()式(/*.....*/);

这篇关于动态加载实体框架中的SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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