在 EF CodeFirst 中动态加载实体配置
我需要根据数据库模式动态地使用 Fluent API 创建复合键。 我用谷歌搜索解决方案但找不到任何解决方案。 有什么建议如何做到这一点吗?
I need to create composite key with fluent API dynamically according to database schema.
I google for the solution but can't find any.
Any suggestion how to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不断发展的数据库=不断发展的类=不断发展的映射。这没有自动化,因为您的类不必是数据库的 1:1 图像,并且由映射来描述它们如何关联。
即使进行 1:1 自动化也是项目本身。这是一项相当复杂的任务,需要花费更长的时间来构建它,然后只需手动改进映射即可。也没有理由这样做,因为它已经存在于 EF 电动工具。
如果您仍然想这样做,请访问 SQL Books online,了解 SQL 如何保留有关表、列、关系、约束等的信息。然后了解如何使用 T4 模板或 CodeDom 生成类。使用这两组信息来获取数据库的描述并相应地创建映射(顺便说一句,如果您想映射到现有的类,这将更加困难。)。
Evolving database = evolving classes = evolving mapping. There is no automation for this because your classes don't have to be 1:1 image of your database and it is up to mapping to describe how they relate.
Even doing 1:1 automation is project itself. It is quite complex task and it will take you much longer to built that then simply evolving your mapping manually. Also there is no reason to do that because it already exists in EF Power Tools.
If you still want to do that go to SQL Books online and learn how SQL persists information about tables, columns, relations, constraints, etc. Then learn how to either use T4 templates or CodeDom to generate classes. Use these two set of information to get description of database and create mapping accordingly (it will be much harder if you would like to do that mapping to existing classes btw.).