播种不适用于 EF 4.1

发布于 2024-12-10 11:05:05 字数 3472 浏览 0 评论 0原文

我在 ASP.NET MVC 教程的帮助下创建了以下代码。 它们位于我称为 Model 的不同类库中。 我在类库中创建了一个 app.config,其中包含名为 HRContext 的连接字符串。 另外,在网络项目上我创建了相同的连接字符串。

当我运行该项目时,我设置了断点,并且 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);
        }
    }

I have the following code I created with help of asp.net mvc tutorials.
These are in a different class library I called Model.
I created an app.config in the class library with the connection string called HRContext.
Also on the webproject I created the same connection string.

When I run the project, I put breakpoints and the Seed method is never executed, however the OnModelCreating is being executed.

I got an exception saying that the table dbo.Position does not exist in the view.

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);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里梦着梦中梦 2024-12-17 11:05:05

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

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

You need to tell EF to use your custom inializer with the Database.SetInitializer method. The best to call it at your application start (if you are using MVC then in the Application_Start). But you should only call it once and before you access your context for the first time

protected void Application_Start()
{
     ...
    Database.SetInitializer(new HRContextInitializer());
     ...
}
宁愿没拥抱 2024-12-17 11:05:05

在您的 HRContext 类中,您需要设置数据库初始值设定项:

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

In your HRContext class you need to set database initializer:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    Database.SetInitializer(new HTContextInitializer());
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文