复杂关系实体框架种子方法

时间:2013-07-05 03:30:38

标签: asp.net c#-4.0 webforms entity-framework-5

我有很多关系,我尝试使用实体框架中的代码优先方法种子。只有最顶层才能正确播种数据。用户类是种子,但基础的对象列表不是。我的Poco如下:

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 = "james_1999@blue.com",
                    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* james_1999@blue.com 2013-07-04 19:34:46.767 2013-07-04 19:34:46.767 1

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

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

Ĵ

1 个答案:

答案 0 :(得分:2)

我建议将其分成几部分:

添加您的用户

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

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

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

(注意:要执行此操作,您需要导航属性,如UserID和模型中的User属性,以便EF知道要与哪个用户相关。就像上面的UserRole模型/示例一样)