0

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.

solution diagram

devlin carnate
  • 8,309
  • 7
  • 48
  • 82

2 Answers2

1

i don't know what caused the problem, but i fixed it by deleting my edmx file and recreating it (add a new edmx, and the wizard asks whether to create from database or create empty. i selected the option to create from database).

before i re-created it, i tried updating the model from the database, and that caused the data model context file (ExternalEmploymentDataModel.Context.cs) to get blanked out. so, something must have been wrong with my data model. after deleting the data model and re-creating it, the context file had content, and i was then able to successfully use the Create() method.

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
0

your EmploymentEntities project must have a reference to applicant project (assembly)

mordechai
  • 829
  • 1
  • 7
  • 23
  • can you please explain? what do you mean by the applicant project assembly? – devlin carnate Jun 19 '14 at 21:08
  • i've updated my original post with the tree diagram of my solution, along with where i have the references. these references were already in place, and the project builds successfully. are you saying i need a reference somewhere else, too? – devlin carnate Jun 19 '14 at 21:24