使用Entity Framework 4在运行时更改数据库模式的最佳实现是什么? [英] What is the best implementation for changing the database schema at runtime using Entity Framework 4?

查看:150
本文介绍了使用Entity Framework 4在运行时更改数据库模式的最佳实现是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用单一数据库,单独模式模型在SQL Server上构建一个ASP.Net MVC 2多租户应用程序,其他地方,这里这里,其中每个租户都有一个或多个用户,并且与其自己的租户具体的SQL Server分开架构。

We are building an ASP.Net MVC 2 multi-tenant application on SQL Server using the "single database, separate schema" model discussed, among other places, here and here, where each tenant has one or more users and is split out with its own, tenant specific, SQL Server schema.

应用程序正在使用Entity Framework 4.为了让不同租户的用户访问其模式中的数据,我们需要能够在创建模式时指定模式实体的ObjectContext。

The application is using Entity Framework 4. In order for users for different tenants to access the data in their schema, we need to be able to specify a schema when creating an entity's ObjectContext.

我已经看到对这个工具的一些引用( http://efmodeladapter.codeplex.com/ ),但是(1)我不需要在使用说明中指定的每次生成EDMX时更新代码,(2)它是在之前生成的EF4,所以我希望现在有一个更简单的方法。

I have seen a few references to this tool (http://efmodeladapter.codeplex.com/) but (1) I would like to not have to update the code every time the EDMX is generated as specified in the Usage instruction and (2) it was produced before EF4 so I am hoping there is an easier way now.

那么在EF4中最好的方法是什么?

So, what is the best way to do this in EF4?

谢谢

推荐答案

嗯,好的。对于未来的搜索者来说,我实现的是一些类似这个例子,尽管我从嵌入式资源中加载ssdl,csdl和msl,如这个例子

Well, ok then. For future searchers, what I have implemented is something like this example, though I load the ssdl, csdl, and msl from the embedded resources like this example.

在实现中,我在MVC操作请求中创建存储库对象时,我使用用户上下文来确定需要哪个模式,使用前面提到的示例来写该模式并创建一个EntityConnection,然后使用它创建我的ObjectContext实体。

In implementation, I when creating a repository object in an MVC action request, I take use a user context to determine what schema is needed, use the previously noted example to write the schema and create an EntityConnection and then use it create my ObjectContext entity.

似乎运行得很好,虽然我有点担心,它直观地看起来像它可能成为一个性能问题。

It seems to function pretty well, though I am a bit concerned that it intuitively seems like it might be a performance problem.

这篇关于使用Entity Framework 4在运行时更改数据库模式的最佳实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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