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);
}
}
You need to tell EF to use your custom inializer with the
Database.SetInitializermethod. 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