在运行时更改实体框架数据库模式 [英] Change Entity framework database schema at runtime

查看:112
本文介绍了在运行时更改实体框架数据库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数asp.net应用程序中,您可以通过在运行时修改connectionstring来更改数据库存储。即,我可以通过简单地更改连接字符串中的数据库字段的值,从使用测试数据库转换为生产数据库。



我正在尝试更改模式(但不一定是数据库本身)与实体框架,但没有运气。



我看到的问题是,edmx xml文件中的SSDL内容正在存储



见下文

 < EntitySet 
Name =task
EntityType =hardModel.Store.task
store:Type =Tables
Schema =test/> ;

现在我已经将模式属性值从test更改为prod p>

但这似乎不是一个很好的解决方案。


  1. 我需要更新evert实体集和存储过程(我有+50表)

  2. 我只能这样做一个编译时间?

  3. 如果我尝试以后更新已经存在的实体模型 - 由于EF不认识到表已经存在于edm中,因此已经存在的实体。

任何想法?

解决方案

更新阅读您的意见后,很明显您想要更改每个DB的引用模式,数据库。我已经编辑了这个问题,以澄清这一点,并恢复您提供的示例EDMX,它隐藏在原始格式中。



我将在下面重复我的评论: / p>


如果模式位于同一个数据库中,您不能在运行时切换(除了EF 4码只)。这是因为两个不同模式中的两个相同命名和结构化的表被认为是完全不同的表。


我也同意上述的JMarsch:我会重新考虑将测试和生产数据(或实际上,任何和生产数据)放在同一个数据库中的设计。似乎是一个灾难的邀请。



以下回答



您确定要更改正确的连接字符串? EF使用的连接字符串在内部嵌入了指定CSDL / SSDL / etc的位置的连接字符串。通常有一个正常连接字符串供您的应用程序的其他部分使用(例如,ASP.NET成员身份)。在这种情况下,更改DB时,您必须更新连接字符串的



同样,如果您在运行时更新连接字符串,则必须使用具体的工具,了解EF连接字符串格式,并与通常的连接字符串构建器分开。请参阅链接中的示例。另请参阅分配EF连接字符串的此帮助


In most asp.net applications you can change the database store by modifing the connectionstring at runtime. i.e I can change from using a test database to a production database by simply changing the value of the "database" field in the connectionstring

I'm trying to change the schema (but not necessarily the database itself) with entity framework but no luck.

The problem I'm seeing is the that the SSDL content in the edmx xml file is storing the schema for each entityset.

see below

<EntitySet 
    Name="task" 
    EntityType="hardModel.Store.task" 
    store:Type="Tables" 
    Schema="test"  />

Now I have changed the schema attribute value to "prod" from test and it works..

But this does not seem to be a good solution.

  1. I need to update evert entity set as well as stored procedures ( I have +50 tables )
  2. I can only do this an compile time?
  3. If I then try to later update the Entity model-entityies that already exist are being read due to EF not recognizing that the table already exists in the edm.

Any thoughts?

解决方案

Update Upon reading your comments it's clear that you're wanting to change the referenced schema for each DB, not the database. I've edited the question to clarify this and to restore the sample EDMX you provided which was hidden in the original formatting.

I'll repeat my comment below here:

If the schemata are in the same DB, you can't switch these at runtime (except with EF 4 code-only). This is because two identically-named and structured tables in two different schemata are considered entirely different tables.

I also agree with JMarsch above: I'd reconsider the design of putting test and production data (or, actually, 'anything and production data') in the same DB. Seems like an invitation to disaster.

Old answer below.

Are you sure you're changing the correct connection string? The connection string used by the EF is embedded inside the connection string which specifies the location of CSDL/SSDL/etc. It's common to have a "normal" connection string for use by some other part of your app (e.g., ASP.NET membership). In this case, when changing DBs you must update both of your connection strings.

Similarly, if you update the connection string at runtime then you must use specific tools for this, which understand the EF connection string format and are separate from the usual connection string builder. See the example in the link. See also this help on assigning EF connection strings.

这篇关于在运行时更改实体框架数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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