Question

I have a fairly standard setup with simply POCO classes

public class Project
{

    public int ProjectId { get; set; }
    public string Name { get; set; }
    public int? ClientId { get; set; }
    public virtual Client Clients { get; set; }
}

They use an interface

public interface IProjectRepository
{
    IEnumerable<Project> Projects { get; }
}

and are constructed as a repository for ninject to bind to

public class EFProjectRepository : IProjectRepository
{
    private EFDbContext context = new EFDbContext();

    public IEnumerable<Project> Projects
    {
        get { return context.Projects; }
    }
}

The actual context is a simply DbContext

public class EFDbContext : DbContext
{
    public DbSet<Project> Projects { get; set; }
}

When I try and enable code first migrations I get the following error

image of error

I have done this exact process with other projects and there as never been an error. This is connecting to a local Sql Server Database. There does not seem to be a problem with the connection string. I have searched for this error online but the solutions seem to answer questions that do not directly relate to my setup.

Was it helpful?

Solution 3

This problem can occur if one of the POCO classes was not declared in the DbContext.

I added them and the error went away

I had changed the name of the Task POCO class because of its association with a built in .NET name System.Threading.Tasks. However I had not changed this in the "TaskTimeLog" POCO where there was a relation. When going through the code the "Task" property in the "TaskTimeLog" POCO was not showing an error because it was now attached to that threading keyword and the reason I had changed the name in the first place.

OTHER TIPS

I had the same issue and the cause was a POCO class that had a property of type Type.

Late to the game...but if it helps...

I had this same problem, everything was working fine, but this issue appeared, I added the following to one of my classes

public HttpPostedFileBase File { get; set; }

which seemed to break it.

I ensured I didn't map this to the database by using the following:

[NotMapped]
public HttpPostedFileBase File { get; set; }

You need to add the following using statement:

using System.ComponentModel.DataAnnotations.Schema;

Hope this helps

I got this error:

Value cannot be null. Parameter name: entitySet

Turns out I was trying to join data from 2 different DbContexts.

        var roles = await _identityDbContext.Roles
            .AsNoTracking()
            .Take(1000)
            .Join(_configurationDbContext.Clients.ToList(),
                a => a.ClientId,
                b => b.Id,
                (a,b) => new {Role = a, Client = b})
            .OrderBy(x => x.Role.ClientId).ThenBy(x => x.Role.Name)
            .Select(x => new RoleViewModel
            {
                Id = x.Role.Id,
                Name = x.Role.Name,
                ClientId = x.Role.ClientId,
                ClientName = x.Client.ClientName
            })
            .ToListAsync();

The fix is to add ToList as shown. Then the join will happen in code instead of the database.

Only do this if you are OK with retrieving the whole table. (I know my "Clients" table will always be relatively small.)

For anyone not finding a resolution in the other answers, I got this error when I created a derived class from a class that had an instance in some model. The exception occurred on the first usage of my context in a request.

This is a stripped-down example that will reproduce the behaviour. Model is a DbSet in my context.

public class Model
{
    public int Id { get; set; }
    public Duration ExposureDuration { get; set; }
}

public class Duration
{
    public int Value { get; set; }
    public string Unit { get; set; }
}

//Adding this will cause the exception to occur.
public class DurationExtended : Duration
{ }

This happened during work in progress. When I changed the model property ExposureDuration to type DurationExtended, all was working again.

I had the same issue and it took quite a while to find out the solution. In our case, we created a seperated project to handle the Entities and even if the default project in the Package Manager Console was the one handling the Entities, I need to set this project as the default project in order to make it work.

I hope this will help somebody else.

I got this error when I declared a variable of type Type - which is probably because is a complex type not supported by the DB.

When I changed it to string, the error went away

public class Sample
{
  public int SampleID {get;set;}
  public Type TypeInfo {get; set;} //This caused the error, 
                                   //because Type is not directly convertible 
                                  //in to a SQL datatype
}

I encountered this same issue and resolved like so:

Error in model class:

public class UserInformation
{
    public string ID { get; set; }
    public string AccountUserName { get; set; }
    public HttpPostedFileBase ProfilePic { get; set; }
}

No error in model class

public class UserInformation
{
    public string ID { get; set; }
    public string AccountUserName { get; set; }
    public string ProfilePicName { get; set; }
}

My issue was resolved once i updated the ProfilePic property type from HttpPostedFileBase to string. If you have a property that is not of type string, int, double or some other basic/standard type either replace such property or update to a type which SQL is more likely to accept.

Remove the line <Generator>EntityModelCodeGenerator</Generator> from your project file.

Check out this https://www.c-sharpcorner.com/UploadFile/5d065a/poco-classes-in-entity-framework/

enter image description here

I have some properties in "ExpenseModel", one of this was...

public virtual Type TypeId {get; set;}

which was causes the above same error because of "Type" propertyType, so I changed
"Type" => "ExpenseType" and it worked... :-)

public virtual ExpenseType TypeId {get; set;}

ExpenseModel.cs

public class ExpenseTypes
{
    [Key]
    public int Id { get; set; }
    public string TypeName { get; set; }
    public string Description { get; set; }
}

In my case I had to reference another model class called IanaTimeZone, but instead of

public virtual IanaTimeZone Timezone { get; set; }

out of rush I typed this:

public virtual TimeZone Timezone { get; set; }

and it compiled fine because VS thought it was System.TimeZone but EF6 was throwing the error. Stupid thing but took me a while to figure out, so maybe this will help someone.

To anyone else this might be helpful, I had a property TimeZone (the actual .NET TimeZone object) and this gave me the exact same error, not sure why but will dig deeper :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top