统一处理多对多表映射

This commit is contained in:
yubaolee
2015-11-30 11:58:18 +08:00
parent 92ead80909
commit 464f04bb32
36 changed files with 968 additions and 3372 deletions

View File

@@ -1,25 +0,0 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using OpenAuth.Domain;
namespace OpenAuth.Repository.Models.Mapping
{
public class ModuleRoleMap : EntityTypeConfiguration<ModuleRole>
{
public ModuleRoleMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
// Table & Column Mappings
this.ToTable("ModuleRole");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.RoleId).HasColumnName("RoleId");
this.Property(t => t.ModuleId).HasColumnName("ModuleId");
this.Property(t => t.Type).HasColumnName("Type");
this.Property(t => t.OperateTime).HasColumnName("OperateTime");
this.Property(t => t.OperatorId).HasColumnName("OperatorId");
}
}
}

View File

@@ -0,0 +1,35 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using OpenAuth.Domain;
namespace OpenAuth.Repository.Models.Mapping
{
public class RelevanceMap : EntityTypeConfiguration<Relevance>
{
public RelevanceMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Description)
.IsRequired()
.HasMaxLength(100);
this.Property(t => t.Key)
.IsRequired()
.HasMaxLength(100);
// Table & Column Mappings
this.ToTable("Relevance");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.FirstId).HasColumnName("FirstId");
this.Property(t => t.SecondId).HasColumnName("SecondId");
this.Property(t => t.Description).HasColumnName("Description");
this.Property(t => t.Key).HasColumnName("Key");
this.Property(t => t.Status).HasColumnName("Status");
this.Property(t => t.OperateTime).HasColumnName("OperateTime");
this.Property(t => t.OperatorId).HasColumnName("OperatorId");
}
}
}

View File

@@ -1,25 +0,0 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using OpenAuth.Domain;
namespace OpenAuth.Repository.Models.Mapping
{
public class UserModuleMap : EntityTypeConfiguration<UserModule>
{
public UserModuleMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
// Table & Column Mappings
this.ToTable("UserModule");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.UserId).HasColumnName("UserId");
this.Property(t => t.ModuleId).HasColumnName("ModuleId");
this.Property(t => t.Type).HasColumnName("Type");
this.Property(t => t.OperateTime).HasColumnName("OperateTime");
this.Property(t => t.OperatorId).HasColumnName("OperatorId");
}
}
}

View File

@@ -1,24 +0,0 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using OpenAuth.Domain;
namespace OpenAuth.Repository.Models.Mapping
{
public class UserOrgMap : EntityTypeConfiguration<UserOrg>
{
public UserOrgMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
// Table & Column Mappings
this.ToTable("UserOrg");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.OrgId).HasColumnName("OrgId");
this.Property(t => t.UserId).HasColumnName("UserId");
this.Property(t => t.OperateTime).HasColumnName("OperateTime");
this.Property(t => t.OperatorId).HasColumnName("OperatorId");
}
}
}

View File

@@ -1,24 +0,0 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using OpenAuth.Domain;
namespace OpenAuth.Repository.Models.Mapping
{
public class UserRoleMap : EntityTypeConfiguration<UserRole>
{
public UserRoleMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
// Table & Column Mappings
this.ToTable("UserRole");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.RoleId).HasColumnName("RoleId");
this.Property(t => t.UserId).HasColumnName("UserId");
this.Property(t => t.OperateTime).HasColumnName("OperateTime");
this.Property(t => t.OperatorId).HasColumnName("OperatorId");
}
}
}

View File

@@ -20,30 +20,24 @@ namespace OpenAuth.Repository.Models
public DbSet<Module> Modules { get; set; }
public DbSet<ModuleElement> ModuleElements { get; set; }
public DbSet<ModuleElementGrant> ModuleElementGrants { get; set; }
public DbSet<ModuleRole> ModuleRoles { get; set; }
public DbSet<Org> Orgs { get; set; }
public DbSet<Relevance> Relevances { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserCfg> UserCfgs { get; set; }
public DbSet<UserExt> UserExts { get; set; }
public DbSet<UserModule> UserModules { get; set; }
public DbSet<UserOrg> UserOrgs { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ModuleMap());
modelBuilder.Configurations.Add(new ModuleElementMap());
modelBuilder.Configurations.Add(new ModuleElementGrantMap());
modelBuilder.Configurations.Add(new ModuleRoleMap());
modelBuilder.Configurations.Add(new OrgMap());
modelBuilder.Configurations.Add(new RelevanceMap());
modelBuilder.Configurations.Add(new RoleMap());
modelBuilder.Configurations.Add(new UserMap());
modelBuilder.Configurations.Add(new UserCfgMap());
modelBuilder.Configurations.Add(new UserExtMap());
modelBuilder.Configurations.Add(new UserModuleMap());
modelBuilder.Configurations.Add(new UserOrgMap());
modelBuilder.Configurations.Add(new UserRoleMap());
}
}
}

View File

@@ -57,23 +57,19 @@
<Compile Include="Models\Mapping\ModuleElementGrantMap.cs" />
<Compile Include="Models\Mapping\ModuleElementMap.cs" />
<Compile Include="Models\Mapping\ModuleMap.cs" />
<Compile Include="Models\Mapping\ModuleRoleMap.cs" />
<Compile Include="Models\Mapping\OrgMap.cs" />
<Compile Include="Models\Mapping\RelevanceMap.cs" />
<Compile Include="Models\Mapping\RoleMap.cs" />
<Compile Include="Models\Mapping\UserCfgMap.cs" />
<Compile Include="Models\Mapping\UserExtMap.cs" />
<Compile Include="Models\Mapping\UserMap.cs" />
<Compile Include="Models\Mapping\UserModuleMap.cs" />
<Compile Include="Models\Mapping\UserOrgMap.cs" />
<Compile Include="Models\Mapping\UserRoleMap.cs" />
<Compile Include="Models\OpenAuthDBContext.cs" />
<Compile Include="OrgRepository.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ModuleRepository.cs" />
<Compile Include="RoleRepository.cs" />
<Compile Include="UserModuleRepository.cs" />
<Compile Include="UserRepository.cs" />
<Compile Include="UserRoleRepository.cs" />
<Compile Include="RelevanceRepository.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj">

View File

@@ -17,9 +17,9 @@ namespace OpenAuth.Repository
public IEnumerable<Org> LoadByUser(int userId)
{
var result = from userorg in Context.UserOrgs
join org in Context.Orgs on userorg.OrgId equals org.Id
where userorg.UserId == userId
var result = from userorg in Context.Relevances
join org in Context.Orgs on userorg.SecondId equals org.Id
where userorg.FirstId == userId && userorg.Key =="UserOrg"
select org;
return result;

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
namespace OpenAuth.Repository
{
public class RelevanceRepository :BaseRepository<Relevance>, IRelevanceRepository
{
public void DeleteBy(string key,params int[] firstIds)
{
Delete(u => firstIds.Contains(u.FirstId) && u.Key == key);
}
public void AddRelevance(string key, Dictionary<int, int> ids)
{
foreach (var roleid in ids)
{
Add(new Relevance
{
Key = key,
FirstId = roleid.Key,
SecondId = roleid.Value,
OperateTime = DateTime.Now
});
}
Save();
}
}
}

View File

@@ -1,51 +0,0 @@
// ***********************************************************************
// Assembly : OpenAuth.Repository
// Author : yubaolee
// Created : 11-26-2015
//
// Last Modified By : yubaolee
// Last Modified On : 11-26-2015
// ***********************************************************************
// <copyright file="UserModuleRepository.cs" company="www.cnblogs.com/yubaolee">
// Copyright (c) www.cnblogs.com/yubaolee. All rights reserved.
// </copyright>
// <summary>用户菜单分配操作</summary>
// ***********************************************************************
using System;
using System.Linq;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
namespace OpenAuth.Repository
{
public class UserModuleRepository : BaseRepository<UserModule>, IUserModuleRepository
{
/// <summary>
/// 删除指定用户关联的模块
/// </summary>
/// <param name="userIds">The user ids.</param>
public void DeleteByUser(params int[] userIds)
{
Delete(u =>userIds.Contains(u.UserId));
}
/// <summary>
/// 为指定的用户分配模块
/// </summary>
public void AddUserModule(int userId, params int[] moduleIds)
{
foreach (var moduleId in moduleIds)
{
Add(new UserModule
{
UserId = userId,
ModuleId = moduleId,
OperateTime = DateTime.Now
});
}
Save();
}
}
}

View File

@@ -21,8 +21,8 @@ namespace OpenAuth.Repository
{
var result = from user in Context.Users
where (
Context.UserOrgs.Where(uo => orgId.Contains(uo.OrgId))
.Select(u => u.UserId)
Context.Relevances.Where(uo => orgId.Contains(uo.SecondId) && uo.Key =="UserOrg")
.Select(u => u.FirstId)
.Distinct()
)
.Contains(user.Id)
@@ -41,32 +41,5 @@ namespace OpenAuth.Repository
return LoadInOrgs(orgIds).OrderBy(u =>u.Id).Skip((pageindex -1)*pagesize).Take(pagesize);
}
/// <summary>
/// 设置用户的机构
/// </summary>
public void SetOrg(int userId, params int[] orgIds)
{
using (TransactionScope ts = new TransactionScope())
{
Context.UserOrgs.Where(u => u.UserId == userId).Delete();
foreach (var orgId in orgIds)
{
Context.UserOrgs.Add(new UserOrg{OrgId = orgId,UserId = userId});
}
Save();
ts.Complete();
}
}
public void Delete(int id)
{
using (TransactionScope ts = new TransactionScope())
{
Context.UserOrgs.Where(u => u.UserId == id).Delete();
Delete(u =>u.Id == id);
ts.Complete();
}
}
}
}

View File

@@ -1,34 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
namespace OpenAuth.Repository
{
public class UserRoleRepository :BaseRepository<UserRole>, IUserRoleRepository
{
public void DeleteByUser(params int[] userIds)
{
Delete(u =>userIds.Contains(u.UserId));
}
public void AddUserRole(int userId, params int[] roleIds)
{
foreach (var roleid in roleIds)
{
Add(new UserRole
{
UserId = userId,
RoleId = roleid,
OperateTime = DateTime.Now
});
}
Save();
}
}
}