I receive the error "The entity type Applicant is not part of the model for the current context" when attempting to Create a new applicant.
I'm adding to an existing project that uses the same database instance, but a different catalog. To do this, I created a separate project in my solution with the data model for the catalog I need to use. I have an app.config file with metadata (csdl, ssdl, msl) that matches the name of my data model and specifies the catalog I need to use. I believe my data model is set up correctly -- when I go to the diagram, right click and select "update model from database", nothing shows as needing to be added, and on the refresh tab, it displays the list of tables from the catalog.
Here is my DbContext:
namespace Employment.Data.External
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class EmploymentEntities : DbContext
{
public EmploymentEntities()
: base("name=EmploymentEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Applicant> Applicants { get; set; }
public DbSet<ApplicantPosting> ApplicantPostings { get; set; }
public DbSet<Availability> Availabilities { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Education> Educations { get; set; }
public DbSet<Employment> Employments { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<Position> Positions { get; set; }
public DbSet<Posting> Postings { get; set; }
public DbSet<Reference> References { get; set; }
}
}
And here is the Create method from my partial class for Applicant:
namespace Employment.Data.External
{
partial class Applicant
{
#region Public Methods
public bool Create()
{
bool isSuccessful = false;
try
{
using (EmploymentEntities entities = new EmploymentEntities())
{
this.ApplicationCreated = DateTime.UtcNow;
entities.Applicants.Add(this);
entities.SaveChanges();
isSuccessful = true;
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
foreach (var entityValidationError in e.EntityValidationErrors)
{
foreach (var error in entityValidationError.ValidationErrors)
{
e.Data.Add(error.PropertyName, error.ErrorMessage);
}
}
throw e;
}
return isSuccessful;
}
The error occurs at "entities.Applicants.Add(this);" And the connection string in my web.config matches the one in my app.config. I don't believe there's confusion between which connection string to use in the web.config, because the code is reaching the project I added with the new DbContext / data model when instantiating EmploymentEntities.
How do I resolve this error?
UPDATE: solution tree diagram, along with where i have the entity references.