Jelajahi Sumber

fix many to many relationships, fix TenantUser mappings

Dalibor Votruba 3 tahun lalu
induk
melakukan
2c3608591f

+ 0 - 1
Common/qdr.fnd.core.data/Repository/EntityRepository.cs

@@ -45,7 +45,6 @@ namespace Quadarax.Foundation.Core.Data.Repository
             {
                 return local.MultiInclude(DefaultIncludes.ToArray());
             }
-
             return DbSetInstance.Page(paging.IsDisabled ? (int?) null : paging.Page, paging.PageSize).MultiInclude(DefaultIncludes.ToArray());
         }
 

+ 8 - 6
Modules/qdr.app.qlbrc.base/Entities/DaoMapper/TenantUserDm.cs

@@ -14,6 +14,7 @@ namespace Quadarax.Application.QLiberace.Base.Entities.DaoMapper
             builder.Entity<TenantUser>(entity =>
                 {
                     entity.ToTable(Constants.Modules.Base.TblTenantUser, Constants.Modules.Base.Schema);
+                    entity.HasKey(tu => new { tu.UserId, tu.TenantId });
 
                     // foreign keys
                     entity.Property(e => e.TenantId).IsRequired()
@@ -21,15 +22,16 @@ namespace Quadarax.Application.QLiberace.Base.Entities.DaoMapper
                     entity.Property(e => e.UserId).IsRequired()
                         .HasComment("Reference to user assigned to tenant");
 
+
                     // relationships
-                    entity.HasOne(d => d.Tenant)
-                        .WithMany()
-                        .HasForeignKey(d => d.TenantId)
+                        entity.HasOne(pc => pc.Tenant)
+                        .WithMany(p => p.Users)
+                        .HasForeignKey(pc => pc.TenantId)
                         .HasConstraintName("REL_TENANTUSER_TENANT_ID");
 
-                    entity.HasOne(d => d.User)
-                        .WithMany()
-                        .HasForeignKey(d => d.UserId)
+                    entity.HasOne(pc => pc.User)
+                        .WithMany(c => c.Tenants)
+                        .HasForeignKey(pc => pc.UserId)
                         .HasConstraintName("REL_TENANTUSER_USER_ID");
 
                 }

+ 2 - 0
Modules/qdr.app.qlbrc.base/Entities/DaoMapper/UserDm.cs

@@ -28,6 +28,8 @@ namespace Quadarax.Application.QLiberace.Base.Entities.DaoMapper
                         .HasMaxLength(200)
                         .HasComment("User login name name. Key search value");
 
+
+                    
                 }
             );
         }

+ 2 - 2
Modules/qdr.app.qlbrc.base/Entities/Tenant.cs

@@ -15,11 +15,11 @@ namespace Quadarax.Application.QLiberace.Base.Entities
         public DateTime? LastAccess { get; set; }
         public bool IsLocked { get; set; }
 
-        
-
+        public virtual ICollection<TenantUser> Users { get; set; }
 
         public Tenant()
         {
+            Users = new List<TenantUser>();
         }
 
     }

+ 2 - 0
Modules/qdr.app.qlbrc.base/Entities/User.cs

@@ -9,9 +9,11 @@ namespace Quadarax.Application.QLiberace.Base.Entities
 
         public bool IsLocked { get; set; }
 
+        public virtual ICollection<TenantUser> Tenants { get; set; }
 
         public User()
         {
+            Tenants = new List<TenantUser>();
         }
 
     }

+ 1 - 0
Modules/qdr.app.qlbrc.base/QlbrcDbContext.cs

@@ -17,6 +17,7 @@ namespace Quadarax.Application.QLiberace.Base
         public DbSet<User> Users { get; set; } = null!;
         public DbSet<Tenant> Tenants { get; set; } = null!;
         public DbSet<Setting> Settings { get; set; } = null!;
+        public DbSet<TenantUser> TenantUsers { get; set; } = null!;
 
         #endregion
         

+ 6 - 3
Modules/qdr.app.qlbrc.base/Repositories/RepoUser.cs

@@ -1,4 +1,5 @@
-using Quadarax.Application.QLiberace.Base.Entities;
+using Microsoft.EntityFrameworkCore;
+using Quadarax.Application.QLiberace.Base.Entities;
 using Quadarax.Foundation.Core.Data;
 using Quadarax.Foundation.Core.Data.Domain;
 using Quadarax.Foundation.Core.Data.Interface.Domain;
@@ -8,6 +9,8 @@ namespace Quadarax.Application.QLiberace.Base.Repositories
 {
     public class RepoUser : EntityRepository<long, User>
     {
+
+
         public RepoUser(IUnitOfWork<IDataDomain> unitOfWork) : base(unitOfWork)
         {
         }
@@ -16,8 +19,8 @@ namespace Quadarax.Application.QLiberace.Base.Repositories
         {
             if (string.IsNullOrEmpty(loginName)) throw new ArgumentNullException(nameof(loginName));
 
-            var query = Query(Paging.NoPaging(), usingUncommited).FirstOrDefault(x =>
-                string.Equals(x.LoginName, loginName, StringComparison.InvariantCultureIgnoreCase) && (x.IsLocked == false || includeLocked == true));
+            var query = Query(Paging.NoPaging(), usingUncommited).Include(x=>x.Tenants).FirstOrDefault(x =>
+                string.Equals(x.LoginName, loginName) && (x.IsLocked == false || includeLocked == true));
             return query;
         }
     }

+ 9 - 5
Workbench/Entity/Tenant.cs

@@ -5,11 +5,7 @@ namespace Workbench.Entity
 {
     public partial class Tenant
     {
-        public Tenant()
-        {
-            Settings = new HashSet<Setting>();
-        }
-
+        
         /// <summary>
         /// Tenant primary key
         /// </summary>
@@ -44,5 +40,13 @@ namespace Workbench.Entity
         public string? Modifier { get; set; }
 
         public virtual ICollection<Setting> Settings { get; set; }
+
+        public ICollection<User> Users { get; set; }
+
+        public Tenant()
+        {
+            Users = new List<User>();
+            Settings = new HashSet<Setting>();
+        }
     }
 }

+ 1 - 1
qdr.app.qlbrc.console/Commands/DbInitCmd.cs

@@ -97,7 +97,7 @@ namespace Quadarax.Application.QLiberace.Console.Commands
                 }
                 else
                 {
-                    WriteCaption($"User: '{user.LoginName}' already exists");
+                    WriteCaption($"User: '{user.LoginName}' already exists with '{user.Tenants.Count}' assignments");
                 }
 
                 WriteInfo($"Assigning user '{user.LoginName}' to tenants..");