播种不适用于 EF 4.1
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要告诉 EF 使用自定义初始化程序
Database.SetInitializer
方法。最好在应用程序启动时调用它(如果您使用 MVC,则在 Application_Start 中)。但你应该只在第一次访问上下文之前调用它一次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在您的 HRContext 类中,您需要设置数据库初始值设定项:
In your HRContext class you need to set database initializer: