简体   繁体   English

实体框架种子方法未运行

[英]Entity Framework Seed method not running

I'm having some trouble getting the Seed method of EF to run. 我在运行EF的Seed方法时遇到了一些麻烦。 I've run update-database in the PMC - but no effect on the DB. 我已经在PMC中运行了update-database-但对数据库没有任何影响。 Here's the method: 方法如下:

public class PhilosopherInitialiser : System.Data.Entity.DropCreateDatabaseIfModelChanges<PhilosopherContext>
        {
            protected override void Seed(PhilosopherContext context)
            {
                var philosophers = new List<Philosopher>{
                    new Philosopher {
                        FirstName = "Bertrand",
                        LastName = "Russell",
                        DateOfBirth = DateTime.Parse("1872-05-18"),
                        DateOfDeath = DateTime.Parse("1970-02-02"),
                        IsAlive = false,
                        NationalityID = 1,
                        AreaID = 7,
                        Description = "Here's some text about Bertrand Russell"
                    },
                    new Philosopher {
                        FirstName = "Immanuel",
                        LastName = "Kant",
                        DateOfBirth = DateTime.Parse("1724-04-22"),
                        DateOfDeath = DateTime.Parse("1804-02-12"),
                        IsAlive = false,
                        NationalityID = 3,
                        AreaID = 1,
                        Description = "Here's some text about Immanuel Kant"
                    },
                    new Philosopher {
                        FirstName = "John",
                        LastName = "Rawls",
                        DateOfBirth = DateTime.Parse("1921-02-21"),
                        DateOfDeath = DateTime.Parse("2002-11-24"),
                        IsAlive = false,
                        NationalityID = 9,
                        AreaID = 3,
                        Description = "Here's some text about John Rawls"
                    }
                };
                philosophers.ForEach(p => context.Philosophers.Add(p));
                context.SaveChanges();

                var nationalities = new List<Nationality>
                {
                    new Nationality { Name = "English" },
                    new Nationality { Name = "Scotish" },
                    new Nationality { Name = "German" },
                    new Nationality { Name = "French" },
                    new Nationality { Name = "Greek" },
                    new Nationality { Name = "Italian" },
                    new Nationality { Name = "Spanish" },
                    new Nationality { Name = "Russian" },
                    new Nationality { Name = "American" }
                };
                nationalities.ForEach(n => context.Nationalities.Add(n));
                context.SaveChanges();

                var areas = new List<Area>{
                    new Area { Name = "Metaphysics" },
                    new Area { Name = "Existentialism" },
                    new Area { Name = "Political philosophy" },
                    new Area { Name = "Philosophy of the mind" },
                    new Area { Name = "Aesthetics" },
                    new Area { Name = "Social philosophy" },
                    new Area { Name = "Logic" },
                    new Area { Name = "Moral philosophy" },
                    new Area { Name = "Epistemology" }
                };
                areas.ForEach(a => context.Areas.Add(a));
                context.SaveChanges();

                var books = new List<Book>
                {
                    new Book {
                        Title = "The impact of science on society",
                        PhilosopherID = 1,
                        AreaID = 6
                    },
                    new Book {
                        Title = "The analysis of mind",
                        PhilosopherID = 1,
                        AreaID = 4
                    },
                    new Book {
                        Title = "Marriage and morals",
                        PhilosopherID = 1,
                        AreaID = 8
                    },
                    new Book{
                        Title = "Critique of pure reason",
                        PhilosopherID = 2,
                        AreaID = 9
                    },
                    new Book{
                        Title = "The metaphysics of morals",
                        PhilosopherID = 2,
                        AreaID = 8
                    },
                    new Book{
                        Title = "A theory of justice",
                        PhilosopherID = 3,
                        AreaID = 3
                    }
                };
                books.ForEach(b => context.Books.Add(b));
                context.SaveChanges();
            }
        }
    }

And here's my PhilosopherContext class: 这是我的PhilosopherContext类:

    public class PhilosopherContext : DbContext
    {
        public PhilosopherContext() : base("PhilosopherContext")
        {
        }

        public DbSet<Philosopher> Philosophers { get; set; }
        public DbSet<Area> Areas { get; set; }
        public DbSet<Nationality> Nationalities { get; set; }
        public DbSet<Book> Books { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Book>()
                .HasRequired(p => p.Philosopher)
                .WithMany()
                .HasForeignKey(p => p.PhilosopherID)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Philosopher>()
                .Property(p => p.DateOfBirth)
                .HasColumnType("datetime2");

            modelBuilder.Entity<Philosopher>()
                .Property(p => p.DateOfDeath)
                .HasColumnType("datetime2");
        }
    }
}

Inside the Web.Config file I'm using initialising the DB here: 在Web.Config文件中,我在这里初始化数据库:

<contexts>
  <context type="PhilosophersLibrary.DAL.PhilosopherContext, PhilosophersLibrary">
    <databaseInitializer type="PhilosophersLibrary.DAL.PhilosopherInitialiser, PhilosophersLibrary" />
  </context>
</contexts>

Does anyone have any suggestions? 有没有人有什么建议? I feel that the method might not be called. 我觉得该方法可能不会被调用。

UPDATE I seem to be making progress. 更新我似乎正在进步。 The Areas and Nationalities tables are being seeded with the data. 区域和国籍表已随数据一起播种。 But I have to comment out the Philosophers data and the Books data. 但是我必须注释掉“哲学家”数据和“书籍”数据。 Is there something wrong with my data model? 我的数据模型有问题吗?

public class Book
{
    public int BookID { get; set; }
    public string Title { get; set; }
    [Display(Name = "Philosopher")]
    public int PhilosopherID { get; set; }
    [Display(Name = "Area")]
    public int AreaID { get; set; }

    public virtual Philosopher Philosopher { get; set; }
    public virtual Area Area { get; set; }
}

public class Philosopher
{
    // <className>ID pattern causes property to be primary key
    public int PhilosopherID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [Display(Name = "Date of birth")]
    public DateTime DateOfBirth { get; set; }
    [Display(Name = "Date of death")]
    public DateTime DateOfDeath { get; set; }
    public Boolean IsAlive { get; set; }
    public string Description { get; set; }

    // Foreign keys have corresponding navigation properties
    // <NavigationProperty>ID naming convention cause EF to identify foreign keys
    public int NationalityID { get; set; }
    public int AreaID { get; set; }

    // Navigation properties - defined as virtual to use LazyLoading
    // Nationality and Area have a 1 to 1 relationship with philosopher
    // Books has a 1 to many relationship with philosopher
    public virtual Nationality Nationality { get; set; }
    public virtual Area Area { get; set; }
    public virtual ICollection<Book> Books { get; set; }
}

Try to use this: 尝试使用此:

CreateDatabaseIfNotExists<PhilosopherContext>

instead of this: 代替这个:

DropCreateDatabaseIfModelChanges<PhilosopherContext>

Also I would add: 我还要补充一点:

public PhilosopherContext() : base("PhilosopherContext")
{
    Database.SetInitializer<PhilosopherContext>(new CreateDatabaseIfNotExists<PhilosopherContext>());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM