简体   繁体   English

使用Entity Framework 4在运行时更改表名

[英]Change table name at runtime using Entity Framework 4

Is it possible to change the name of the table a specific entity is mapped to in Entity Framework 4 (model first)? 是否可以在Entity Framework 4(模型优先)中更改特定实体映射到的表的名称? I need to create one model per customer and thus need to prefix the table names. 我需要为每个客户创建一个模型,因此需要为表名添加前缀。 Maybe simpler to use one database per customer. 每个客户使用一个数据库可能更简单。 What is the best practice? 什么是最佳做法?

Use database per customer. 每个客户使用数据库。 Databases must have same schema. 数据库必须具有相同的架构。 Modifying mapping at runtime is not a good idea. 在运行时修改映射不是一个好主意。

Edit: 编辑:

Model per customer means EDMX per customer. 每个客户的模型意味着每个客户的EDMX。 Be aware that metadata are shared. 请注意,元数据是共享的。 So in multi-tenant application (that is how I understand your description) every change to metadata will change metadata for every request. 因此,在多租户应用程序(这就是我理解您的描述)中,对元数据的每次更改都将更改每个请求的元数据。 Also using separate database looks like better architecture in terms of security and performance. 在安全性和性能方面,使用单独的数据库看起来也更好。

Another question is if metadata can be changed at runtime. 另一个问题是,是否可以在运行时更改元数据。 Metadata are stored in MetadataWorkspace . 元数据存储在MetadataWorkspace You can read them but I'm not sure if you can also modify them (I will check it during weekend). 你可以阅读它们,但我不确定你是否也可以修改它们(我会在周末检查它)。

EF-generated model is generated based on your database schema. EF生成的模型基于您的数据库模式生成。 I don't see how you can alter the schema within EF. 我不知道如何在EF中改变模式。 Does not compute in my little head. 不计算在我的小脑袋里。

With that said, you can perhaps write a stored procedure that alters the table, and call that stored proc via your EF context. 话虽如此,您也许可以编写一个改变表的存储过程,并通过EF上下文调用该存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM