Value cannot be null. Parameter name: entitySet

前端 未结 12 1791
夕颜
夕颜 2020-12-03 17:02

I have a fairly standard setup with simply POCO classes

public class Project
{

    public int ProjectId { get; set; }
    public string Name { get; set; }
          


        
相关标签:
12条回答
  • 2020-12-03 17:17

    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.

    0 讨论(0)
  • 2020-12-03 17:17

    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 :)

    0 讨论(0)
  • 2020-12-03 17:20

    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.)

    0 讨论(0)
  • 2020-12-03 17:25

    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.

    0 讨论(0)
  • 2020-12-03 17:25

    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; }
    }
    
    0 讨论(0)
  • 2020-12-03 17:26

    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

    0 讨论(0)
提交回复
热议问题