【问题标题】:Complex Relationships Entity Framework Seed Method复杂关系实体框架种子方法
【发布时间】:2013-07-05 03:30:37
【问题描述】:

我有一些多对多的关系,我试图在实体框架中使用代码优先方法来播种。只有最顶层实际上是正确地播种数据。用户类正在播种,但对象的底层列表不是。我的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

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

非常感谢任何帮助!再次感谢大家。

,

J

【问题讨论】:

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


    【解决方案1】:

    我建议分段处理:

    添加您的用户

    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 模型/示例中一样)

    【讨论】:

    • 这种方法效果很好。谢谢你。关于这是否被视为最佳做法的任何想法?
    • 我喜欢它,因为它非常模块化,我可以轻松定位和更改特定表的种子。就最佳实践而言,这似乎是Programmer Stack Exchange 的问题。
    猜你喜欢
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多