EF 4.1搞乱了事情。 FK命名策略有改变吗? [英] EF 4.1 messing things up. Has FK naming strategy changed?

查看:73
本文介绍了EF 4.1搞乱了事情。 FK命名策略有改变吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了新的Entity Framework 4.1 NuGet软件包,因此根据NuGet impuctions 替换 EFCodeFirst 软件包 Scott Hanselman的这篇文章



现在,想象下面的模型:

  public class User 
{
[Key]
public string UserName {get;组; }
// any
}

public class UserThing
{
public int ID {get;组; }
public virtual User User {get;组; }
//任何
}

最后一个EFCodeFirst发布产生一个外键在 UserThing 表中调用 UserUserName



安装后新版本和运行我得到以下错误:

 无效列名'User_UserName'

当然这意味着新版本具有不同的FK命名策略。这在所有其他表和列中是一致的:无论FK EFCodeFirst命名为 AnyOldForeignKeyID EF 4.1想要调用 AnyOldForeignKey_ID (注意下划线)。



我不介意用下划线命名FK,但在这种情况下,这意味着不得不不必要地丢弃数据库并重新创建或不必要地重命名有没有人知道为什么FK命名约定已经改变,以及是否可以配置而不使用Fluent API

p>

解决方案

不幸的是,在这个版本中没有的事情之一是能够在Code First中添加自定义约定: / p>

http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-release-candidate-available.aspx



如果你不想使用t他流畅的API来配置列名(我不怪你),那么最直接的方法是使用 sp_rename


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?

解决方案

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.

这篇关于EF 4.1搞乱了事情。 FK命名策略有改变吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆