EF 4.1 messing things up. Has FK naming strategy changed?

前端 未结 2 987
灰色年华
灰色年华 2020-12-17 21:35

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 Sc

相关标签:
2条回答
  • 2020-12-17 22:02

    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"));
      }
    }
    
    0 讨论(0)
  • 2020-12-17 22:12

    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.

    0 讨论(0)
提交回复
热议问题