EF 4.1 把事情搞砸了。 FK命名策略有变化吗?

发布于 2024-10-24 18:45:41 字数 950 浏览 1 评论 0原文

我刚刚安装了新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-10-31 18:45:41

不幸的是,此版本中没有包含在 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.

情场扛把子 2024-10-31 18:45:41

为什么不执行以下操作呢?

  public class User
  {
    [Key]
    public string UserName { get; set; }
    // whatever
  }

  public class UserThing
  {
    public int ID { get; set; }
    public string UserUserName { get; set; }
    [ForeignKey("UserUserName")]
    public virtual User User { get; set; }
    // whatever
  }

或者,如果您不想将 UserUserName 属性添加到 UserThing,则使用 Fluent API,如下所示:

// class User same as in question
// class UserThing same as in question

public class MyContext : DbContext
{
  public MyContext()
    : base("MyCeDb") { }
  public DbSet<User> Users { get; set; }
  public DbSet<UserThing> UserThings { get; set; }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<UserThing>()
      .HasOptional(ut => ut.User)    // See if HasRequired fits your model better
      .WithMany().Map(u => u.MapKey("UserUserName"));
  }
}

Why don't you do the following?

  public class User
  {
    [Key]
    public string UserName { get; set; }
    // whatever
  }

  public class UserThing
  {
    public int ID { get; set; }
    public string UserUserName { get; set; }
    [ForeignKey("UserUserName")]
    public virtual User User { get; set; }
    // whatever
  }

Or, if you don't want to add the UserUserName property to UserThing, then use the fluent API, like so:

// class User same as in question
// class UserThing same as in question

public class MyContext : DbContext
{
  public MyContext()
    : base("MyCeDb") { }
  public DbSet<User> Users { get; set; }
  public DbSet<UserThing> UserThings { get; set; }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<UserThing>()
      .HasOptional(ut => ut.User)    // See if HasRequired fits your model better
      .WithMany().Map(u => u.MapKey("UserUserName"));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文