EF 4.1 把事情搞砸了。 FK命名策略有变化吗?
我刚刚安装了新的 Entity Framework 4.1 NuGet 包,因此根据 NuGet 指令和Scott Hanselman 的这篇文章。
现在,想象一下以下模型:
public class User
{
[Key]
public string UserName { get; set; }
// whatever
}
public class UserThing
{
public int ID { get; set; }
public virtual User User { get; set; }
// whatever
}
最后一个 EFCodeFirst 版本在 UserThing
表中生成了一个名为 UserUserName
的外键。
安装新版本并运行后,出现以下错误:
Invalid column name 'User_UserName'
这当然意味着新版本具有不同的 FK 命名策略。这在所有其他表和列中是一致的:无论名为 AnyOldForeignKeyID
的 FK EFCodeFirst EF 4.1 想要调用 AnyOldForeignKey_ID
(请注意下划线)。
我不介意用下划线命名 FK,但在这种情况下,这意味着必须要么不必要地丢弃数据库并重新创建它,要么不必要地重命名所有 FK。
有谁知道为什么 FK 命名约定发生了变化以及是否可以在不使用 Fluent API 的情况下配置它?
I've just installed the new Entity Framework 4.1 NuGet package, thus replacing the EFCodeFirst
package as per NuGet intructions and this article of Scott Hanselman.
Now, imagine the following model:
public class User
{
[Key]
public string UserName { get; set; }
// whatever
}
public class UserThing
{
public int ID { get; set; }
public virtual User User { get; set; }
// whatever
}
The last EFCodeFirst release generated a foreign key in the UserThing
table called UserUserName
.
After installing the new release and running I get the following error:
Invalid column name 'User_UserName'
Which of course means that the new release has a different FK naming strategy. This is consistent among all other tables and columns: whatever FK EFCodeFirst named AnyOldForeignKeyID
EF 4.1 wants to call AnyOldForeignKey_ID
(note the underscore).
I don't mind naming the FK's with an underscore, but in this case it means having to either unnecessarily throw away the database and recreate it or unnecessarily renaming al FK's.
Does any one know why the FK naming convention has changed and whether it can be configured without using the Fluent API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,此版本中没有包含在 Code First 中添加自定义约定的功能:
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1 -release-candidate-available.aspx
如果您不想使用流畅的 API 来配置列名称(我不怪您),那么最直接的方法可能是使用 < a href="http://msdn.microsoft.com/en-us/library/ms188351.aspx" rel="nofollow">sp_rename。
Unfortunately, one of the things that didn't make it to this release is the ability to add custom conventions in Code First:
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-release-candidate-available.aspx
If you don't want to use the fluent API to configure the column name (which I don't blame you), then most straight forward way to do it is probably using sp_rename.
为什么不执行以下操作呢?
或者,如果您不想将 UserUserName 属性添加到 UserThing,则使用 Fluent API,如下所示:
Why don't you do the following?
Or, if you don't want to add the UserUserName property to UserThing, then use the fluent API, like so: