Reputation: 13367
I asked a question earlier which was answered here
Basically I have 2 classes that are needed:
public class User : IUser
{
public string Id { get; set; }
// Stripped for brevity
public IList<Role> Roles { get; set; }
public User()
{
this.Id = Guid.NewGuid().ToString();
}
}
public class Role : IRole
{
public string Id { get; set; }
public string Name { get; set; }
public Role()
{
this.Id = Guid.NewGuid().ToString();
}
}
In my DbContext I have this declaration:
modelBuilder.Entity<User>()
.HasMany(m => m.Roles)
.WithMany()
.Map(m => {
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
m.ToTable("UserRoles");
});
That all works perfectly. Now, I want to add a user to a specific role, so I created a service following my service / repository / unit of work design pattern like this:
public class UserRoleService : Service<UserRole>
{
public UserRoleService(IUnitOfWork unitOfWork)
: base(unitOfWork)
{
}
public async Task<IList<UserRole>> GetAllAsync(string userId, params string[] includes)
{
return await this.Repository.GetAll(includes).Where(m => m.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase)).ToListAsync();
}
public async Task CreateAsync(UserRole model)
{
var isInRole = await IsInRole(model.UserId, model.RoleId);
if (isInRole)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.UserInRole, new object[] { model.RoleId }));
this.Repository.Create(model);
}
public async Task RemoveAsync(UserRole model)
{
var isInRole = await IsInRole(model.UserId, model.RoleId);
if (!isInRole)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.UserNotInRole, new object[] { model.RoleId }));
this.Repository.Remove(model);
}
public async Task<bool> IsInRole(string userId, string roleId)
{
if (string.IsNullOrEmpty(userId))
throw new ArgumentNullException("userId");
if (string.IsNullOrEmpty(userId))
throw new ArgumentNullException("roleId");
var roles = await this.GetAllAsync(userId);
var match = roles.Where(m => m.RoleId.Equals(roleId, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
return (match == null);
}
}
The inherited service class looks like this:
public class Service<T> where T : class
{
private readonly IRepository<T> repository;
protected IRepository<T> Repository
{
get { return this.repository; }
}
public Service(IUnitOfWork unitOfWork)
{
if (unitOfWork == null)
throw new ArgumentNullException("unitOfWork");
this.repository = unitOfWork.GetRepository<T>();
}
}
Now, because my DbContext doesn't have a DbSet for UserRoles it complains, so I added one. Then I got a complaint that the new table does not have a primary key set up, so I added one. So now my DbContext looks like this:
public class DatabaseContext : DbContext
{
// Removed for brevity
public DbSet<Role> Roles { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
static DatabaseContext()
{
//Database.SetInitializer<IdentityContext>(null); // Exsting database, do nothing (Comment out to create database)
}
public DatabaseContext()
: base("DefaultConnection")
{
base.Configuration.LazyLoadingEnabled = false; // Disable Lazy Loading
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); // Remove Cascading Delete
// Removed from brevity
modelBuilder.Entity<UserRole>().HasKey(m => new { m.UserId, m.RoleId });
modelBuilder.Entity<User>()
.HasMany(m => m.Roles)
.WithMany()
.Map(m => {
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
m.ToTable("UserRoles");
});
}
}
The problem I have now, is that my code creates two tables for UserRole called UserRoles and UserRoles1. Can someone tell me how to get it to only use one table?
Upvotes: 0
Views: 5274
Reputation: 10591
If UserRole represents a pure mapping (i.e. it contains only UserId and RoleId, and no other properties), you do not actually need a UserRole object. It is enough to define the relationship in your DbContext (which you have done). In order to associate a particular role with a particular user, you can simply do:
user.Roles.Add(role);
...and commit the DbSet
Entity Framework is smart enough to maintain the many-to-many mapping table for you without actually having an entity that represents the mapping itself.
Note: The role object you are trying to add needs to be entity from your database context. If you try to create the role entity and then assign, EF will try to insert (and probably fail due to primary key violation).
Upvotes: 5