播种不适用于EF 4.1

时间:2011-10-17 09:30:15

标签: entity-framework entity-framework-4 entity-framework-4.1

我在asp.net mvc教程的帮助下创建了以下代码。 这些是在我称为模型的不同类库中。 我在类库中使用名为HRContext的连接字符串创建了一个app.config。 同样在webproject上我创建了相同的连接字符串。

当我运行项目时,我放置断点并且从不执​​行Seed方法,但是正在执行OnModelCreating。

我得到一个例外,说视图中不存在表dbo.Position。

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Position'.



Line 21:             var positions = unitOfWork.PositionRepository.Find(p => p.PositionID != null);
Line 22: 
Line 23:             return View(positions.ToList());
Line 24:         }








 public class HRContextInitializer : DropCreateDatabaseAlways<HRContext>
    {
        protected override void Seed(HRContext context)
        {
            Position netdeveloper = new Position() { name = ".net developer", yearsExperienceRequired = 5 };
            Position javadeveloper = new Position() { name = "java developer", yearsExperienceRequired = 5 };

            byte[] johnImage = File.ReadAllBytes(@"\Content\Photos\1.jpg");
            byte[] luisImage = File.ReadAllBytes(@"\Content\Photos\2.jpg");

            Applicant luis = new Applicant()
            {
                name = "Luis",
                skypeuser = "le.valencia",
                telephone = "0491732825",
                photo = luisImage
            };

            Applicant john = new Applicant()
            {
                name = "John",
                skypeuser = "jo.valencia",
                telephone = "3435343543",
                photo = johnImage
            };


            ApplicantPosition appicantposition = new ApplicantPosition()
            {
                Applicant = luis,
                Position = netdeveloper,
                appliedDate = DateTime.Today,
                Status = Status.Applied
            };

            ApplicantPosition appicantposition2 = new ApplicantPosition()
            {
                Applicant = john,
                Position = javadeveloper,
                appliedDate = DateTime.Today,
                Status = Status.Applied
            };

            context.Positions.Add(netdeveloper);
            context.Positions.Add(javadeveloper);
            context.Applicants.Add(luis);
            context.Applicants.Add(john);
            context.ApplicantsPositions.Add(appicantposition);

            context.SaveChanges();
        }
    }




public class HRContext : DbContext
    {
        public DbSet<Position> Positions { get; set; }
        public DbSet<Applicant> Applicants { get; set; }
        public DbSet<ApplicantPosition> ApplicantsPositions { get; set; }
        public DbSet<ApplicationPositionHistory> ApplicationsPositionHistory { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Position>().ToTable("Position");
            modelBuilder.Entity<Applicant>().ToTable("Applicant");
            modelBuilder.Entity<ApplicantPosition>().ToTable("ApplicantPosition");
            modelBuilder.Entity<ApplicationPositionHistory>().ToTable("ApplicationsPositionHistory");

            modelBuilder.Entity<Position>().Property(c => c.name).IsRequired();
            modelBuilder.Entity<Applicant>().Property(c => c.name).IsRequired();
            modelBuilder.Entity<ApplicantPosition>().Property(c => c.appliedDate).IsRequired();

            base.OnModelCreating(modelBuilder);
        }
    }

2 个答案:

答案 0 :(得分:2)

您需要告诉EF使用Database.SetInitializer方法使用您的自定义inializer。最好在应用程序启动时调用它(如果您在Application_Start中使用MVC)。但是你应该只在第一次访问你的上下文之前调用它一次

protected void Application_Start()
{
     ...
    Database.SetInitializer(new HRContextInitializer());
     ...
}

答案 1 :(得分:1)

在HRContext类中,您需要设置数据库初始化程序:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    Database.SetInitializer(new HTContextInitializer());
}