EF 4.1,POCO:有什么方法可以在运行时获取表名称以避免硬编码?
我在实体框架中使用 POCO。最新的 EF 版本中是否有任何直接或间接的方法可以在运行时获取表名称以避免硬编码值?
我需要它在我的自定义数据库初始化程序中运行这样的代码:
context.Database.ExecuteSqlCommand(
string.Format("DBCC CHECKIDENT ({0}, RESEED, {1})", tableName, newSeed))
谢谢
I use POCO in Entity Framework. Is any direct or indirect way in the latest EF version to get Table name at the runtime to avoid hardcode values?
I need it inside my custom database initializer to run code like this:
context.Database.ExecuteSqlCommand(
string.Format("DBCC CHECKIDENT ({0}, RESEED, {1})", tableName, newSeed))
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您的上下文与我的上下文类似,当您将 DbSet 添加到上下文时,每个表名都是从类名生成的。如果是这种情况,您可以通过反射来实现您的目标,尽管它有点难看:
更新:我删除了复数黑客,只是关闭了生成的表名称中的复数(请参阅 OnModelCreating 覆盖)。
I'm working from the assumption that your context looks something like mine, with each of the table names getting generated from the class names when you add a DbSet to your context. If that's the case, you can achieve your goal with reflection, though it's a little ugly:
UPDATE: I removed the pluralization hack and just turned off pluralization in the generated table names (see the OnModelCreating override).
POCO 意味着您可以在数据模型中使用“普通”CLR 对象 (POCO),例如现有的域对象。这些 POCO 数据类(也称为持久性无知对象)映射到数据模型中定义的实体,并且根据定义,它不应与数据库实现细节直接相关。但是,您可以使用常量类和 Fluent 映射来以更好的方式满足您的需求
您的常量类实现
您的映射就像这样
在您的 dbInitializer 中
POCO means you can use "plain-old" CLR objects (POCO), such as existing domain objects, with your data model. These POCO data classes (also known as persistence-ignorant objects), which are mapped to entities that are defined in a data model and by definition it shouldn't be directly related to database implementation details. However, you can use constant class and Fluent mapping to facilitate your requirement in a better way
Your constant class implementation
Your mappings goes like this
In your dbInitializer
看看这个讨论有多“活跃”,在我看来,当前版本的 EF 中并未提供此功能。我希望此功能将在 EF 的未来版本之一中提供。
Looking at how "active" this discussion is, it seems to me this functionality is just not provided in the current version of EF. I hope this features will be available in one of future version of EF.
这段代码到底有用吗?
Will this code be useful at all?