Steve Lydford
Steve Lydford

Reputation: 428

Entity Framework Code First Error - EntityType 'Variable' has no key defined

I'm using Entity Framework 5 Code First in an ASP.NET MVC 4 project and I'm getting the following error:

One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Variable' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Variables' is based on type 'Variable' that has no keys defined.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Variable' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Variables' is based on type 'Variable' that has no keys defined.

This has been driving me nuts for the last hour. I have searched my solution and have nothing called 'Variable' at all!

My context looks like this:

public class MyContext : DbContext, IDisposedTracker
{

    public DbSet<Project> Projects { get; set; }

    public bool IsDisposed { get; set; }
}

My model looks like this:

public class Project
{
    [Key]
    public int Uid { get; set; }

    public string Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public List<Task> Tasks { get; internal set; }
}

Task looks like this:

public class Task
{
    [Key]
    public int Uid { get; set; }

    public string Id;

    public string Name;
}

Upvotes: 0

Views: 7942

Answers (1)

Jason Portnoy
Jason Portnoy

Reputation: 797

You're asking for an relationship with no table to relate to.

There needs to be a place to place the object Tasks

public class MyContext : DbContext, IDisposedTracker
{

    public DbSet<Project> Projects { get; set; }
    public DbSet<Task> Tasks { get; set; }
    public bool IsDisposed { get; set; }
}

Upvotes: 1

Related Questions