Entity Framework from many to several to one entity

I am trying to map a property for a user who has many, many relationships in the database, but only one for each user. But I am unable to identify the required card in the entity. I have the following objects:

public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    //Need to map this property
    public virtual SecurityRole SecurityRole { get; set; }
}

public class SecurityRole
{
    public int Id { get; set; }
    public string Name { get; set; }
}

      

The following tables:

User:
    Id
    FirstName
    LastName
SecurityRole:
    Id
    Name
UserSecurityRole:
    UserId
    SecurityRoleId

      

If anyone has an idea or can point me in the right direction that would be great

+3


source to share


1 answer


Even if there is only one record in the database, if you have many different relationships between User

and SecurityRole

, it should work like this:



public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public List<SecurityRole> SecurityRoles { get; set; }
}

public class SecurityRole
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List<User> Users { get; set; }
}

      

+1


source







All Articles