0

I have this POCO class for User which has and Id and a collection of RoleUser objects among other properties

public class SystemUser : IEntity
{
    public int Id { get; set; }

    [MaxLength(50)]
    [Index("ExternalUserIdIndex",IsUnique = true)]
    public string ExternalUserId { get; set; }
    public virtual ICollection<RoleUser> UserRoles { get; set; }
}

My RoleUser class is this

public class RoleUser : IEntity
{
    public int Id { get; set; }
    public int SystemUserId { get; set; }
    public virtual Role Role { get; set; }
}

The Role class is not relevant for this question. I have some seeding method for adding default users with some roles, before adding roles I delete existing roles. I was trying to do the following:

var user = context.SystemUsers.FirstOrDefault(u => u.ExternalUserId == "extId");

As you can see I get this instance as tracked by my dbContext. Then first I tried:

user.UserRoles.Clear();
//add roles

But I got this when running my seed method:

The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

Then I tried

user.UserRoles.ToList().ForEach(r => developerPaco.UserRoles.Remove(r));

An got same error. Finally I did:

user.UserRoles.ToList().ForEach(r=>context.RoleUsers.Remove(r)); 

And it worked. So I want to understand why it did not work the other way.

Note: I got it working with my first two attempts by changing my RoleUser class like the following:

public class RoleUser : IEntity
{
    public int Id { get; set; }
    public int? SystemUserId { get; set; }
    public virtual Role Role { get; set; }
}

Making nullable SystemUserId, but then I just get data with null values and not deleted as I wanted.

halfer
  • 19,824
  • 17
  • 99
  • 186
taquion
  • 2,667
  • 2
  • 18
  • 29
  • 1
    You do not need the RoleUser class. Build a Many-To-Many relation between SystemUser and the UserRole. You can do that also without navigation properties. See http://stackoverflow.com/questions/16718699/map-many-to-many-relationship-without-navigation-property – Sir Rufo Jan 10 '17 at 22:55
  • Thanks. But supouse I still want to know why my code does not work – taquion Jan 10 '17 at 23:17

0 Answers0