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?
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
{
...
}
}
You have ICollection<UInfo>
as property, and you are using new UInfo
in the code. It should be new List<UInfo>
.