Value cannot be null. Parameter name: entitySet

有些话、适合烂在心里 提交于 2019-11-27 14:58:21

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

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.

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.

Looking4Answers

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/

Amol Teli

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.

I got this error:

Value cannot be null. Parameter name: entitySet

Turns out I way 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!