brockallen / BrockAllen.MembershipReboot

MembershipReboot is a user identity management and authentication library.
Other
742 stars 238 forks source link

Creating a custom UserAccount with collection property causes EF exception on CreateAccount #569

Closed kareemaismail closed 8 years ago

kareemaismail commented 8 years ago

Hi

In order to reproduce this issue, following the provided sample, "CustomUserAccount", change the following: 1) Create a new class, Role

public class Role { [Key] public Guid Id { get; set; } = Guid.NewGuid(); public virtual string Name { get; set; } }

2) Add a collection of Roles to the CustomUserAccount class:

public class CustomUserAccount : RelationalUserAccount { // make sure the custom properties are all virtual public virtual int Age { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual List Roles { get; set; } }

3) Create the DbSet in the Context:

public class CustomDb : MembershipRebootDbContext { public CustomDb() : base("CustomDatabase") { }

    public DbSet<Role> SystemRoles { get; set; }
}

4) Running this sample app will now run to the line account = svc.CreateAccount at which point an InvalidOperationException is thrown in EntityFramework with the following message:

{"The source query for this EntityCollection or EntityReference cannot be returned when the related object is in either an added state or a detached state and was not originally retrieved using the NoTracking merge option."}

Is there anything I can do to avoid this? Thanks

dealproc commented 8 years ago

first thoughts... how are you mapped? are you mapping like @brockallen has the entity framework package setup? If not, you may want to mimic that functionality and see if you have it resolved. Brock had a blog post about OR/Ms and how Entity Framework really doesn't behave like one, and what needs to be done in order to get it to behave more like one.

brockallen commented 8 years ago

I've never tried to extend MR's user account class with custom collections so I really don't know if it's possible and/or if it'd work. Usually if you need other tables, you do this in a custom DbContext layer (with new DbSets). If you get it working keep us posted.

Also, why are you adding roles separately and not just using the built-in claims support? Roles are just claims.