Entity Framework virtual ICollections

asp.net-core entity-framework-core

Question

I'm new to .NET Core Entity Framework (code-first), but do daily progress. I am now stuck on a probably small mistake and can't go on.

I have a class list that I am trying to fill and in there there is a virtual ICollection from another list, to be filled at the same time.

These are my classes

    public class UgInfo
    {
        public Guid UserGroupId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public virtual ICollection<UInfo> UInfo { get; set; }
    }

    public class UInfo
    {
        public string UserEmail { get; set; }
        public string UserName { get; set; }
    }

This is where the error occurs:

Invalid Initializer member declarator"

Code:

        var ugList = (from ug in _context.Usergroups
                      join uug in _context.UserUsergroup on ug.UserGroupId equals uug.UsergroupId
                      join u in _context.Users on uug.UserId equals u.UserId
                      select new UgInfo
                      {
                          UserGroupId = uug.UsergroupId,
                          Description = ug.Description,
                          Name = ug.Name,
                          new UInfo //Error
                          {
                              UserName = u.UserName,
                              UserEmail = u.Email
                          }

                      }).ToList();

        return ugList;

Could there be anyone who can help a beginner?

1
0
9/25/2018 2:38:35 PM

Accepted Answer

Well, you're missing the member name you're attempting to initialize, for one. Then, you need to initialize it with a collection type, not a single UInfo instance:

...
Name = ug.Name,
UInfo = new List<UInfo> 
{
    new UInfo
    {
        ...
    }
}
0
9/25/2018 2:29:05 PM

Popular Answer

You have ICollection<UInfo> as property, and you are using new UInfo in the code. It should be new List<UInfo>.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow