如何在代码中动态地将表添加到 EF4 上下文 - 无代码优先
我们每 6 个月运行一系列报告,并将结果存储到表中,以便将来随时查询/查看。根据周期,将添加两个或四个表。它们具有 yyyy_mmm_Table_x 标准命名约定。
我们的网站是使用 ASP.Net MVC2 构建的,数据库是使用标准模型设计器(而不是 Code First)使用 EF4 建模的。
我希望能够在运行时将报告表动态添加到 EF4 上下文。我不想使用设计器手动将它们添加到模型中,否则每个报告周期我们都必须更新并重新编译模型,只是因为我们添加了额外的报告。当其他方面都没有改变时,这将是一个令人头痛的维护问题。
我只需通过查询 sysobjects 即可获取可用表的列表。如果我可以获取此列表并将表添加到站点启动时的上下文中,那么我可以使用动态 LINQ 库之类的东西根据用户从下拉列表中选择的表来查询它们。
我无法开箱即用地使用 EF4 的 Code First,因为这将迫使我为表创建具体的类,而这将是同样令人头疼的维护问题。我怀疑我可以使用 Code First 框架用来动态更新上下文的相同策略,但我根本没有看过这个库,我希望熟悉它的人能给我指出正确的方向。
否则我想我必须回到 ADO.Net 来处理这个区域。这可能是最好、最简单的方法,所以我想我正在寻找评论。我不是狂热分子,所以我不需要将所有内容都放在 LINQ 和 EF4 中。 :) 但它似乎更干净和一致,特别是如果它允许我使用动态 LINQ。但有时旧方法更简单。
因此,如果您有任何建议或意见,我很乐意听到。
谢谢!
We run a series of reports every 6 months and store the results to tables that can be queried/viewed at any time in the future. Depending on the cycle either two or four tables will be added. They have a standard naming convention of yyyy_mmm_Table_x.
Our website is built using ASP.Net MVC2 and the database is modeled using EF4 using the standard model designer, not Code First.
I would like to be able to dynamically add the report tables to the EF4 context at runtime. I don't want to have to manually add them to the model using the designer, otherwise every reporting cycle we have to update and recompile the model just because we added the extra reports. That would be a maintenance headache when nothing else has changed.
I can get a list of the available tables simply by querying sysobjects. If I could get this list and add the tables to the context when the site started up then I could use something like the Dynamic LINQ library to query against them depending on which table the user selected from a dropdown.
I can't use EF4's Code First out of the box because that would force me to create concrete classes for the tables and that would just be the same maintenance headache. I suspect I could use the same strategies the Code First framework uses to dynamically update the context, but I haven't looked at this library at all and I'm hoping someone familiar with it can point me in the right direction.
Otherwise I think I would have to drop back to ADO.Net to handle this area. That may be the best and simple way so I guess I'm looking for comments. I'm not a zealot so I don't need everything to be in LINQ and EF4. :) But it would seem to be a little cleaner and consistent, especially if it allows me to make use of Dynamic LINQ. But sometimes the old way is just simpler.
So, if you have any suggestions or comments I would love to hear them.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使使用常见的 EF,您仍然需要为每个表添加新的数据类型,因为当您映射表时,您需要新实体类型的新
ObjectSet
才能运行查询。据我所知,即使表结构完全相同,也不可能将两个表映射到同一实体。所有运行时映射都存储在
EntityConnection
准备的MetadataWorkspace
中。因此,如果您想使用它,您可以从那里开始,但这些类的公共接口看起来不太有希望。我猜您想在这些表上运行 Linq-to-entities,因此使用存储过程根据数据参数从正确的表返回数据可能不是一个选项。
为此,您应该使用通用的 ADO.NET。
Even with common EF you still need new data type for each table because when you map the table you need new
ObjectSet
of new entity type to be able to run queries. As I know it is not possible to map two tables to the same entity even if table structure is absolutely same.All runtime mapping is stored in
MetadataWorkspace
prepared byEntityConnection
. So if you want to play with it you can start there but public interfaces of these classes don't look promising.I guess you want to run Linq-to-entities on these tables so using Stored procedure returning data from correct table based on data parameter is probably not an option.
You should use common ADO.NET for this.