使用 Entity Framework 4 在运行时更改表名称
是否可以更改实体框架 4 中特定实体映射到的表的名称(模型优先)?我需要为每个客户创建一个模型,因此需要为表名称添加前缀。每个客户使用一个数据库可能更简单。最佳实践是什么?
Is it possible to change the name of the table a specific entity is mapped to in Entity Framework 4 (model first)? 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个客户使用数据库。数据库必须具有相同的架构。在运行时修改映射不是一个好主意。
编辑:
每个客户的模型意味着每个客户的 EDMX。请注意,元数据是共享的。因此,在多租户应用程序中(这就是我对您的描述的理解),对元数据的每次更改都会更改每个请求的元数据。在安全性和性能方面,使用单独的数据库看起来也是更好的架构。
另一个问题是元数据是否可以在运行时更改。元数据存储在
MetadataWorkspace
中。您可以阅读它们,但我不确定您是否也可以修改它们(我将在周末检查)。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. 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
. You can read them but I'm not sure if you can also modify them (I will check it during weekend).EF 生成的模型是根据您的数据库架构生成的。我不明白如何更改 EF 中的架构。我的小脑袋里不会计算。
话虽如此,您也许可以编写一个更改表的存储过程,并通过 EF 上下文调用该存储过程。
EF-generated model is generated based on your database schema. I don't see how you can alter the schema within 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.