2013-07-05 43 views
0

我有一些多对多的关系,我试图在实体框架中使用代码优先的方法种子。只有最上面的层实际上正确地播种数据。用户类正在播种,但底层的对象列表不是。是我波索的如下:复杂关系实体框架种子方法

public class User:Database 
    { 
     public String FirstName { get; set; } 
     public String LastName { get; set; } 
     public String UserName { get; set; } 
     public String Password { get; set; } 
     public String Email { get; set; } 

     public virtual List<Address> Addresses { get; set; } 
     public virtual List<UserRole> UserRoles { get; set; } 

    } 
public class Address:Database 
    { 
     public String City { get; set; } 
     public String State { get; set; } 
     public int Zip { get; set; } 
     public String StreetAddress1 { get; set; } 
     public String StreetAddress2 { get; set; } 

     public int CountryID { get; set; } 
     public virtual Country Country { get; set; } 

     public int AddressTypeID { get; set; } 
     public virtual AddressType Type { get; set; } 


    } 

public class UserRole:Database 
    { 
     public int UserID { get; set; } 
     public virtual User User { get; set; } 

     public int RoleID { get; set; } 
     public virtual Role Role { get; set; } 
    } 

public class Role:Database 
    { 
     public String Name { get; set; } 
    } 

public abstract class Database 
    { 
     public int ID { get; set; } 
     public DateTime CreatedAt { get; set; } 
     public DateTime UpdatedAt { get; set; } 
     public Boolean IsActive { get; set; } 

    } 

我已构建的种子的方法如下:

protected override void Seed(CodeFirstODS.DAL.DBContext.Context context) 
     { 
      // This method will be called after migrating to the latest version. 

      // You can use the DbSet<T>.AddOrUpdate() helper extension method 
      // to avoid creating duplicate seed data. E.g. 
      // 
      // context.People.AddOrUpdate(
      //  p => p.FullName, 
      //  new Person { FullName = "Andrew Peters" }, 
      //  new Person { FullName = "Brice Lambson" }, 
      //  new Person { FullName = "Rowan Miller" } 
      // ); 
      // 

      context.Users.AddOrUpdate(
       u => u.ID, 
       new Models.User { 
        ID=1, 
        FirstName="James", 
        LastName = "Rainey",      
        UserName = "BloodyRu1n", 
        Password = "bobcat*", 
        Email = "[email protected]", 
        Addresses = new List<Models.Address>(){ 
         new Models.Address(){ 
          ID = 1, 
          StreetAddress1 = "1260 Monkey Knoll", 
          City = "Marietta", 
          State = "Geaorgia", 
          Country = new Models.Country(){ 
           ID = 1, 
           Name = "USA", 
           CreatedAt = DateTime.Now, 
           UpdatedAt = DateTime.Now, 
           IsActive = true 
          }, 
          Type = new Models.AddressType(){ 
           ID = 1, 
           Name = "Home", 
           CreatedAt = DateTime.Now, 
           UpdatedAt = DateTime.Now, 
           IsActive = true 
          }, 
          CreatedAt = DateTime.Now, 
          UpdatedAt = DateTime.Now, 
          IsActive = true 
         } 
        }, 
        UserRoles = new List<Models.UserRole>(){ 
         new Models.UserRole(){ 
          Role = new Models.Role(){ 
           ID = 1, 
           Name = "Admin", 
           CreatedAt = DateTime.Now, 
           UpdatedAt = DateTime.Now, 
           IsActive = true 
          }        
         }, 
         new Models.UserRole(){ 
          Role = new Models.Role(){ 
           ID = 1, 
           Name = "Admin", 
           CreatedAt = DateTime.Now, 
           UpdatedAt = DateTime.Now, 
           IsActive = true 
          } 
         }, 
        }, 
        CreatedAt = DateTime.Now, 
        UpdatedAt = DateTime.Now, 
        IsActive = true 
       } 
      );//End User 1 Seed 
      context.SaveChanges(); 
     } 

再次将用户数据在数据库中正确地传播,但是,对象的所有基础列表不在数据库中传播。所有的桌子都如预期一般。 Context类如下。

public class Context:DbContext 
    { 

     public DbSet<User> Users { get; set; } 
     public DbSet<UserRole> UserRoles { get; set; } 
     public DbSet<Role> Roles { get; set; } 
     public DbSet<Address> Addresses { get; set; } 
     public DbSet<AddressType> AddressTypes { get; set; } 
     public DbSet<Country> Countries { get; set; } 

    } 

数据库就像这样。

ID FirstName LastName UserName Password Email CreatedAt UpdatedAt IsActive 
1 James Rainey BloodyRu1n bobcat* [email protected] 2013-07-04 19:34:46.767 2013-07-04 19:34:46.767 1 

地址和其他对象表都没有插入值。

任何帮助非常感谢!再次感谢你们。

Ĵ

回答

2

我会在片建议接近这一点:

添加用户

context.Users.AddOrUpdate(
     u => u.ID, 
     new Models.User { 
      ID=1, 
      FirstName="James", 
      LastName = "Rainey",      
      UserName = "BloodyRu1n", 
      Password = "bobcat*", 
      Email = "[email protected]",      
      CreatedAt = DateTime.Now, 
      UpdatedAt = DateTime.Now, 
      IsActive = true 
     } 
);//End User 1 Seed 

然后添加相关数据,如地址,角色等的UserRole让EF建立表格之间的连接:

context.UserRoles.AddOrUpdate(
     r => r.ID,   
     new Models.UserRole{ 
      UserID = 1, 
      //etc 
     }, 
     new Models.UserRole{ 
      UserID = 2, 
      //etc 
     }, 
}; //End roles seed 

(注意:为此,您需要导航属性,如UserID和用户属性,以便EF知道与哪个用户相关联。就像在上面的UserRole模型中一样)

+0

这种方法运行良好。谢谢。关于这是否被认为是最佳做法的任何想法? – user1345632

+0

我喜欢它,因为它非常模块化,我可以轻松找到并更改特定表的种子。就最佳实践而言,这似乎是[程序员堆栈交换](http://programmers.stackexchange.com/)的问题。 – holbrookbw1