From 6c2389bd2e4e8f0c98617fdb28ab96421beca9c7 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Thu, 22 Oct 2020 19:10:41 -0300
Subject: [PATCH] commit

---
 .../Dtos/CourseCreateDto.cs                   |   4 +
 .../Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs |   1 +
 .../Dtos/MessageCreateDto.cs                  |   3 +
 .../Dtos/UserRegisterDto.cs                   |   4 +
 .../Interfaces/ICourseService.cs              |   2 +-
 .../Interfaces/IEmailService.cs               |   2 +-
 .../Interfaces/IMessageService.cs             |   2 +-
 .../Interfaces/IUserService.cs                |   4 +-
 .../Services/CourseService.cs                 |   4 +-
 .../Services/EmailService.cs                  |   6 +-
 .../Services/MessageService.cs                |   6 +-
 .../Services/PostMessageService.cs            |   3 +-
 .../Services/UserService.cs                   |  17 +-
 Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs    |   5 +
 Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs     |   4 -
 Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs   |   4 +-
 .../Tsi1.DataLayer/Entities/PostMessage.cs    |   5 +-
 Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs |   4 +
 Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs   |   5 +
 Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs    |  10 +-
 Tsi1.Api/Tsi1.DataLayer/Entities/User.cs      |  11 +
 .../CourseConfiguration.cs                    |   6 +-
 .../EntityConfiguration/ForumConfiguration.cs |   6 +-
 .../EntityConfiguration/PostConfiguration.cs  |   2 +-
 .../ProfessorConfiguration.cs                 |   6 +-
 .../StudentConfiguration.cs                   |   6 +-
 .../EntityConfiguration/UserConfiguration.cs  |   6 +-
 ...201022010216_add-multy-tenancy.Designer.cs | 489 ++++++++++++++++++
 .../20201022010216_add-multy-tenancy.cs       | 373 +++++++++++++
 .../Migrations/Tsi1ContextModelSnapshot.cs    | 117 ++++-
 30 files changed, 1071 insertions(+), 46 deletions(-)
 create mode 100644 Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.Designer.cs
 create mode 100644 Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.cs

diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs
index ef582f9..ffd3608 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs
@@ -1,11 +1,15 @@
 using System;
 using System.Collections.Generic;
 using System.Text;
+using System.Text.Json.Serialization;
 
 namespace Tsi1.BusinessLayer.Dtos
 {
     public class CourseCreateDto
     {
         public string Name { get; set; }
+
+        [JsonIgnore]
+        public int TenantId { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs
index 2756562..b4ebbec 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs
@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Text;
+using System.Text.Json.Serialization;
 
 namespace Tsi1.BusinessLayer.Dtos
 {
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/MessageCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/MessageCreateDto.cs
index 628b095..399bc0c 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/MessageCreateDto.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/MessageCreateDto.cs
@@ -13,5 +13,8 @@ namespace Tsi1.BusinessLayer.Dtos
         public int ReceiverId { get; set; }
 
         public string Content { get; set; }
+
+        [JsonIgnore]
+        public int TenantId { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs
index 24616b3..2cc5d27 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs
@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.Text;
+using System.Text.Json.Serialization;
 
 namespace Tsi1.BusinessLayer.Dtos
 {
@@ -28,6 +29,9 @@ namespace Tsi1.BusinessLayer.Dtos
         [Required]
         public string IdentityCard { get; set; }
 
+        [JsonIgnore]
+        public int TenantId { get; set; }
+
         public int Age { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
index df6907f..d99c841 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
@@ -10,7 +10,7 @@ namespace Tsi1.BusinessLayer.Interfaces
 {
     public interface ICourseService
     {
-        Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType);
+        Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId);
 
         Task<ServiceResult<Course>> Create(CourseCreateDto newCourse);
     }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs
index 4f68ea6..a97ea5c 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs
@@ -13,6 +13,6 @@ namespace Tsi1.BusinessLayer.Interfaces
     {
         Task<ServiceResult<bool>> SendEmailAsync(MimeMessage message);
 
-        Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> users);
+        Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> mails);
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IMessageService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IMessageService.cs
index 785b461..f328cf4 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IMessageService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IMessageService.cs
@@ -9,7 +9,7 @@ namespace Tsi1.BusinessLayer.Interfaces
 {
     public interface IMessageService
     {
-        Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId);
+        Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId, int tenantId);
 
         Task<ServiceResult<MessagePreviewDto>> Send(MessageCreateDto newMessage);
 
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
index 3e67427..8b2f9d7 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs
@@ -10,10 +10,10 @@ namespace Tsi1.BusinessLayer.Interfaces
 {
     public interface IUserService
     {
-        Task<ServiceResult<User>> Authenticate(string username, string password);
+        Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId);
 
         Task<ServiceResult<User>> Create(UserRegisterDto dto, string type);
 
-        Task<ServiceResult<List<UserPreviewDto>>> GetAll();
+        Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId);
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
index 05c1757..266341d 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
@@ -37,7 +37,7 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
-        public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType)
+        public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId)
         {
             var result = new ServiceResult<List<CoursePreviewDto>>();
             result.Data = new List<CoursePreviewDto>();
@@ -48,6 +48,7 @@ namespace Tsi1.BusinessLayer.Services
             {
                 courses = await _context.StudentCourses
                     .Include(x => x.Course)
+                    .Where(x => x.Course.TenantId == tenantId)
                     .Select(x => x.Course)
                     .ToListAsync();
             }
@@ -55,6 +56,7 @@ namespace Tsi1.BusinessLayer.Services
             {
                 courses = await _context.ProfessorCourses
                     .Include(x => x.Course)
+                    .Where(x => x.Course.TenantId == tenantId)
                     .Select(x => x.Course)
                     .ToListAsync();
             }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs
index fad2cf7..d00e398 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs
@@ -70,13 +70,13 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
-        public async Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto,  List<string> users)
+        public async Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto,  List<string> mails)
         {
             var message = new MimeMessage();
 
-            foreach (var user in users)
+            foreach (var mail in mails)
             {
-                message.To.Add(MailboxAddress.Parse(user));
+                message.To.Add(MailboxAddress.Parse(mail));
             }
 
             message.Subject = $"Nuevo Post: {postCreateDto.Title}";
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/MessageService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/MessageService.cs
index eab3cb3..b49bf79 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/MessageService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/MessageService.cs
@@ -28,14 +28,14 @@ namespace Tsi1.BusinessLayer.Services
             _mapper = mapper;
         }
 
-        public async Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId)
+        public async Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId, int tenantId)
         {
             var result = new ServiceResult<List<MessagePreviewDto>>();
 
             var sort = Builders<Message>.Sort.Ascending("Date");
 
-            var messages = await _messages.Find(m => (m.SenderId == userId && m.ReceiverId == otherUserId)
-                                        || (m.SenderId == otherUserId && m.ReceiverId == userId))
+            var messages = await _messages.Find(m => (m.SenderId == userId && m.ReceiverId == otherUserId && m.TenantId == tenantId)
+                                        || (m.SenderId == otherUserId && m.ReceiverId == userId && m.TenantId == tenantId))
                                         .Sort(sort)
                                         .ToListAsync();
 
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs
index 067a331..5385c44 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs
@@ -45,7 +45,8 @@ namespace Tsi1.BusinessLayer.Services
         {
             var result = new ServiceResult<PostMessage>();
 
-            var postMessage = await _context.PostMessages.FirstOrDefaultAsync(x => x.Id == postMessageId);
+            var postMessage = await _context.PostMessages
+                .FirstOrDefaultAsync(x => x.Id == postMessageId);
 
             if (postMessage == null)
             {
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
index e824af2..b78defc 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs
@@ -25,13 +25,13 @@ namespace Tsi1.BusinessLayer.Services
             _mapper = mapper;
         }
 
-        public async Task<ServiceResult<User>> Authenticate(string username, string password)
+        public async Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId)
         {
             var result = new ServiceResult<User>();
 
             var user = await _context.Users
                 .Include(x => x.UserType)
-                .FirstOrDefaultAsync(x => x.Username == username);
+                .FirstOrDefaultAsync(x => x.Username == username && x.TenantId == tenantId);
 
             if (user == null)
             {
@@ -63,7 +63,8 @@ namespace Tsi1.BusinessLayer.Services
                 Password = dto.Password,
                 Email = dto.Email,
                 FirstName = dto.FirstName,
-                LastName = dto.LastName
+                LastName = dto.LastName,
+                TenantId = dto.TenantId
             };
 
             if (type == UserTypes.Student)
@@ -71,7 +72,8 @@ namespace Tsi1.BusinessLayer.Services
                 user.Student = new Student()
                 {
                     IdentityCard = dto.IdentityCard,
-                    Age = dto.Age
+                    Age = dto.Age,
+                    TenantId = dto.TenantId
                 };
             }
 
@@ -79,7 +81,8 @@ namespace Tsi1.BusinessLayer.Services
             {
                 user.Professor = new Professor()
                 {
-                    IdentityCard = dto.IdentityCard
+                    IdentityCard = dto.IdentityCard,
+                    TenantId = dto.TenantId
                 };
             }
 
@@ -90,12 +93,12 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
-        public async Task<ServiceResult<List<UserPreviewDto>>> GetAll()
+        public async Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId)
         {
             var result = new ServiceResult<List<UserPreviewDto>>();
 
             var users = await _context.Users
-                .Where(x => x.UserType.Name != UserTypes.FacultyAdmin)
+                .Where(x => x.UserType.Name != UserTypes.FacultyAdmin && x.TenantId == tenantId)
                 .ToListAsync();
 
             var usersDto = _mapper.Map<List<UserPreviewDto>>(users);
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs
index c2ee29e..0f4f050 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs
@@ -14,8 +14,13 @@ namespace Tsi1.DataLayer.Entities
         }
 
         public int Id { get; set; }
+        
         public string Name { get; set; }
 
+        public int TenantId { get; set; }
+
+        public Tenant Tenant { get; set; }
+
         public ICollection<StudentCourse> StudentCourses { get; set; }
         public ICollection<ProfessorCourse> ProfessorCourses { get; set; }
         public ICollection<Forum> Forums { get; set; }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs
index 4eda36d..197e1e0 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs
@@ -18,10 +18,6 @@ namespace Tsi1.DataLayer.Entities
 
         public int CourseId { get; set; }
 
-        public int TenantId { get; set; }
-
-        public Tenant Tenant { get; set; }
-
         public Course Course { get; set; }
 
         public ICollection<Post> Posts { get; set; }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs
index 4925699..76a37fc 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs
@@ -18,7 +18,9 @@ namespace Tsi1.DataLayer.Entities
 
         public string Content { get; set; }
 
-        public DateTime Date { get; set; } 
+        public DateTime Date { get; set; }
+
+        public int TenantId { get; set; }
 
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/PostMessage.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/PostMessage.cs
index 0e73c2a..9363833 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/PostMessage.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/PostMessage.cs
@@ -13,11 +13,10 @@ namespace Tsi1.DataLayer.Entities
         public DateTime Date { get; set; }
 
         public int UserId { get; set; }
-
-        public User User { get; set; }
-
+      
         public int PostId { get; set; }
 
         public Post Post { get; set; }
+        public User User { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs
index a095017..9fb5aee 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs
@@ -12,8 +12,12 @@ namespace Tsi1.DataLayer.Entities
         }
 
         public int Id { get; set; }
+        
         public string IdentityCard { get; set; }
 
+        public int TenantId { get; set; }
+
+        public Tenant Tenant { get; set; }
         public User User { get; set; }
         public ICollection<ProfessorCourse> ProfessorCourses { get; set; }
     }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs
index acc8572..2688e37 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs
@@ -12,9 +12,14 @@ namespace Tsi1.DataLayer.Entities
         }
 
         public int Id { get; set; }
+        
         public string IdentityCard { get; set; }
+        
         public int Age { get; set; }
 
+        public int TenantId { get; set; }
+
+        public Tenant Tenant { get; set; }
         public User User { get; set; }
         public ICollection<StudentCourse> StudentCourses { get; set; }
     }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs
index d5201ec..ce807be 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs
@@ -8,13 +8,19 @@ namespace Tsi1.DataLayer.Entities
     {
         public Tenant()
         {
-            Forums = new HashSet<Forum>();
+            Courses = new HashSet<Course>();
+            Professors = new HashSet<Professor>();
+            Students = new HashSet<Student>();
+            Users = new HashSet<User>();
         }
 
         public int Id { get; set; }
 
         public string Name { get; set; }
 
-        public ICollection<Forum> Forums { get; set; }
+        public ICollection<Course> Courses { get; set; }
+        public ICollection<Professor> Professors { get; set; }
+        public ICollection<Student> Students { get; set; }
+        public ICollection<User> Users { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs
index dd7e804..a62c466 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs
@@ -14,15 +14,26 @@ namespace Tsi1.DataLayer.Entities
         }
 
         public int Id { get; set; }
+        
         public int UserTypeId { get; set; }
+        
         public int? StudentId { get; set; }
+        
         public int? ProfessorId { get; set; }
+        
         public string Username { get; set; }
+        
         public string Password { get; set; }
+        
         public string FirstName { get; set; }
+        
         public string LastName { get; set; }
+        
         public string Email { get; set; }
 
+        public int TenantId { get; set; }
+
+        public Tenant Tenant { get; set; }
         public UserType UserType { get; set; }
         public Student Student { get; set; }
         public Professor Professor { get; set; }
diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/CourseConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/CourseConfiguration.cs
index 2bec81e..4cea401 100644
--- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/CourseConfiguration.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/CourseConfiguration.cs
@@ -13,12 +13,16 @@ namespace Tsi1.DataLayer.EntityConfiguration
         {
             builder.HasKey(x => x.Id);
 
-            builder.HasIndex(x => x.Name)
+            builder.HasIndex(x => new { x.Name, x.TenantId })
                 .IsUnique();
 
             builder.Property(x => x.Name)
                 .IsRequired()
                 .HasColumnType("character varying(50)");
+
+            builder.HasOne(x => x.Tenant)
+                .WithMany(x => x.Courses)
+                .HasForeignKey(x => x.TenantId);
         }
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs
index 1de5bbd..ac2d370 100644
--- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs
@@ -14,7 +14,7 @@ namespace Tsi1.DataLayer.EntityConfiguration
         {
             builder.HasKey(x => x.Id);
 
-            builder.HasIndex(x => new { x.CourseId, x.Name })
+            builder.HasIndex(x => new { x.CourseId, x.Name})
                 .IsUnique();
 
             builder.Property(x => x.Name)
@@ -25,10 +25,6 @@ namespace Tsi1.DataLayer.EntityConfiguration
                 .WithMany(x => x.Forums)
                 .HasForeignKey(x => x.CourseId);
 
-            builder.HasOne(x => x.Tenant)
-                .WithMany(x => x.Forums)
-                .HasForeignKey(x => x.TenantId);
-
         }
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/PostConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/PostConfiguration.cs
index 6635726..9ae3c71 100644
--- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/PostConfiguration.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/PostConfiguration.cs
@@ -14,7 +14,7 @@ namespace Tsi1.DataLayer.EntityConfiguration
         {
             builder.HasKey(x => x.Id);
 
-            builder.HasIndex(x => new { x.ForumId, x.Title })
+            builder.HasIndex(x => new { x.ForumId, x.Title})
                 .IsUnique();
 
             builder.Property(x => x.Title)
diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ProfessorConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ProfessorConfiguration.cs
index 679dae3..47de03f 100644
--- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ProfessorConfiguration.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ProfessorConfiguration.cs
@@ -13,12 +13,16 @@ namespace Tsi1.DataLayer.EntityConfiguration
         {
             builder.HasKey(x => x.Id);
 
-            builder.HasIndex(x => x.IdentityCard)
+            builder.HasIndex(x => new { x.IdentityCard, x.TenantId })
                 .IsUnique();
 
             builder.Property(x => x.IdentityCard)
                 .IsRequired()
                 .HasColumnType("character varying(50)");
+
+            builder.HasOne(x => x.Tenant)
+                .WithMany(x => x.Professors)
+                .HasForeignKey(x => x.TenantId);
         }
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/StudentConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/StudentConfiguration.cs
index a7877ac..7e841db 100644
--- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/StudentConfiguration.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/StudentConfiguration.cs
@@ -13,7 +13,7 @@ namespace Tsi1.DataLayer.EntityConfiguration
         {
             builder.HasKey(x => x.Id);
 
-            builder.HasIndex(x => x.IdentityCard)
+            builder.HasIndex(x => new { x.IdentityCard, x.TenantId })
                 .IsUnique();
 
             builder.Property(x => x.IdentityCard)
@@ -22,6 +22,10 @@ namespace Tsi1.DataLayer.EntityConfiguration
 
             builder.Property(x => x.Age)
                 .IsRequired();
+
+            builder.HasOne(x => x.Tenant)
+                .WithMany(x => x.Students)
+                .HasForeignKey(x => x.TenantId);
         }
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/UserConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/UserConfiguration.cs
index eadf441..c10f560 100644
--- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/UserConfiguration.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/UserConfiguration.cs
@@ -13,7 +13,7 @@ namespace Tsi1.DataLayer.EntityConfiguration
         {
             builder.HasKey(x => x.Id);
 
-            builder.HasIndex(x => x.Username)
+            builder.HasIndex(x => new { x.Username, x.TenantId })
                 .IsUnique();
 
             builder.Property(x => x.Username)
@@ -43,6 +43,10 @@ namespace Tsi1.DataLayer.EntityConfiguration
             builder.HasOne(x => x.Professor)
                 .WithOne(x => x.User)
                 .HasForeignKey<User>(x => x.ProfessorId);
+
+            builder.HasOne(x => x.Tenant)
+                .WithMany(x => x.Users)
+                .HasForeignKey(x => x.TenantId);
         }
     }
 }
diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.Designer.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.Designer.cs
new file mode 100644
index 0000000..61c250c
--- /dev/null
+++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.Designer.cs
@@ -0,0 +1,489 @@
+// <auto-generated />
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+using Tsi1.DataLayer;
+
+namespace Tsi1.DataLayer.Migrations
+{
+    [DbContext(typeof(Tsi1Context))]
+    [Migration("20201022010216_add-multy-tenancy")]
+    partial class addmultytenancy
+    {
+        protected override void BuildTargetModel(ModelBuilder modelBuilder)
+        {
+#pragma warning disable 612, 618
+            modelBuilder
+                .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)
+                .HasAnnotation("ProductVersion", "3.1.4")
+                .HasAnnotation("Relational:MaxIdentifierLength", 63);
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("character varying(50)");
+
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("Name", "TenantId")
+                        .IsUnique();
+
+                    b.ToTable("Courses");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<int>("CourseId")
+                        .HasColumnType("integer");
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("character varying(50)");
+
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("CourseId", "Name", "TenantId")
+                        .IsUnique();
+
+                    b.ToTable("Forums");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b =>
+                {
+                    b.Property<int>("ForumId")
+                        .HasColumnType("integer");
+
+                    b.Property<int>("UserId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("ForumId", "UserId");
+
+                    b.HasIndex("UserId");
+
+                    b.ToTable("ForumUsers");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Post", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<DateTime>("Date")
+                        .HasColumnType("timestamp without time zone");
+
+                    b.Property<int>("ForumId")
+                        .HasColumnType("integer");
+
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
+                    b.Property<string>("Title")
+                        .IsRequired()
+                        .HasColumnType("character varying(100)");
+
+                    b.Property<int>("UserId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("UserId");
+
+                    b.HasIndex("ForumId", "Title", "TenantId")
+                        .IsUnique();
+
+                    b.ToTable("Posts");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.PostMessage", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<string>("Content")
+                        .IsRequired()
+                        .HasColumnType("character varying(10485760)");
+
+                    b.Property<DateTime>("Date")
+                        .HasColumnType("timestamp without time zone");
+
+                    b.Property<int>("PostId")
+                        .HasColumnType("integer");
+
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
+                    b.Property<int>("UserId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("PostId");
+
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("UserId");
+
+                    b.ToTable("PostMessages");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<string>("IdentityCard")
+                        .IsRequired()
+                        .HasColumnType("character varying(50)");
+
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("IdentityCard", "TenantId")
+                        .IsUnique();
+
+                    b.ToTable("Professors");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b =>
+                {
+                    b.Property<int>("ProfessorId")
+                        .HasColumnType("integer");
+
+                    b.Property<int>("CourseId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("ProfessorId", "CourseId");
+
+                    b.HasIndex("CourseId");
+
+                    b.ToTable("ProfessorCourses");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<int>("Age")
+                        .HasColumnType("integer");
+
+                    b.Property<string>("IdentityCard")
+                        .IsRequired()
+                        .HasColumnType("character varying(50)");
+
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("IdentityCard", "TenantId")
+                        .IsUnique();
+
+                    b.ToTable("Students");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b =>
+                {
+                    b.Property<int>("StudentId")
+                        .HasColumnType("integer");
+
+                    b.Property<int>("CourseId")
+                        .HasColumnType("integer");
+
+                    b.HasKey("StudentId", "CourseId");
+
+                    b.HasIndex("CourseId");
+
+                    b.ToTable("StudentCourses");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Tenant", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("character varying(50)");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("Name")
+                        .IsUnique();
+
+                    b.ToTable("Tenants");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<string>("Email")
+                        .IsRequired()
+                        .HasColumnType("character varying(255)");
+
+                    b.Property<string>("FirstName")
+                        .IsRequired()
+                        .HasColumnType("character varying(255)");
+
+                    b.Property<string>("LastName")
+                        .IsRequired()
+                        .HasColumnType("character varying(255)");
+
+                    b.Property<string>("Password")
+                        .IsRequired()
+                        .HasColumnType("character varying(255)");
+
+                    b.Property<int?>("ProfessorId")
+                        .HasColumnType("integer");
+
+                    b.Property<int?>("StudentId")
+                        .HasColumnType("integer");
+
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
+                    b.Property<int>("UserTypeId")
+                        .HasColumnType("integer");
+
+                    b.Property<string>("Username")
+                        .IsRequired()
+                        .HasColumnType("character varying(50)");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("ProfessorId")
+                        .IsUnique();
+
+                    b.HasIndex("StudentId")
+                        .IsUnique();
+
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("UserTypeId");
+
+                    b.HasIndex("Username", "TenantId")
+                        .IsUnique();
+
+                    b.ToTable("Users");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.UserType", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("character varying(50)");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("Name")
+                        .IsUnique();
+
+                    b.ToTable("UserTypes");
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Courses")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
+                        .WithMany("Forums")
+                        .HasForeignKey("CourseId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Forums")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum")
+                        .WithMany("ForumUsers")
+                        .HasForeignKey("ForumId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.User", "User")
+                        .WithMany("ForumUsers")
+                        .HasForeignKey("UserId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Post", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum")
+                        .WithMany("Posts")
+                        .HasForeignKey("ForumId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Posts")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.User", "User")
+                        .WithMany("Posts")
+                        .HasForeignKey("UserId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.PostMessage", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Post", "Post")
+                        .WithMany("PostMessages")
+                        .HasForeignKey("PostId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("PostMessages")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.User", "User")
+                        .WithMany("PostMessages")
+                        .HasForeignKey("UserId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Professors")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
+                        .WithMany("ProfessorCourses")
+                        .HasForeignKey("CourseId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.Professor", "Professor")
+                        .WithMany("ProfessorCourses")
+                        .HasForeignKey("ProfessorId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Students")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
+                        .WithMany("StudentCourses")
+                        .HasForeignKey("CourseId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.Student", "Student")
+                        .WithMany("StudentCourses")
+                        .HasForeignKey("StudentId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Professor", "Professor")
+                        .WithOne("User")
+                        .HasForeignKey("Tsi1.DataLayer.Entities.User", "ProfessorId");
+
+                    b.HasOne("Tsi1.DataLayer.Entities.Student", "Student")
+                        .WithOne("User")
+                        .HasForeignKey("Tsi1.DataLayer.Entities.User", "StudentId");
+
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Users")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.UserType", "UserType")
+                        .WithMany()
+                        .HasForeignKey("UserTypeId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+#pragma warning restore 612, 618
+        }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.cs
new file mode 100644
index 0000000..c205fce
--- /dev/null
+++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.cs
@@ -0,0 +1,373 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+namespace Tsi1.DataLayer.Migrations
+{
+    public partial class addmultytenancy : Migration
+    {
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropIndex(
+                name: "IX_Users_Username",
+                table: "Users");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Students_IdentityCard",
+                table: "Students");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Professors_IdentityCard",
+                table: "Professors");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Posts_ForumId_Title",
+                table: "Posts");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Forums_CourseId_Name",
+                table: "Forums");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Courses_Name",
+                table: "Courses");
+
+            migrationBuilder.AddColumn<int>(
+                name: "TenantId",
+                table: "Users",
+                nullable: false,
+                defaultValue: 0);
+
+            migrationBuilder.AddColumn<int>(
+                name: "TenantId",
+                table: "Students",
+                nullable: false,
+                defaultValue: 0);
+
+            migrationBuilder.AddColumn<int>(
+                name: "TenantId",
+                table: "Professors",
+                nullable: false,
+                defaultValue: 0);
+
+            migrationBuilder.AddColumn<int>(
+                name: "TenantId",
+                table: "Posts",
+                nullable: false,
+                defaultValue: 0);
+
+            migrationBuilder.AddColumn<int>(
+                name: "TenantId",
+                table: "PostMessages",
+                nullable: false,
+                defaultValue: 0);
+
+            migrationBuilder.AddColumn<int>(
+                name: "TenantId",
+                table: "Forums",
+                nullable: false,
+                defaultValue: 0);
+
+            migrationBuilder.AddColumn<int>(
+                name: "TenantId",
+                table: "Courses",
+                nullable: false,
+                defaultValue: 0);
+
+            migrationBuilder.CreateTable(
+                name: "Tenants",
+                columns: table => new
+                {
+                    Id = table.Column<int>(nullable: false)
+                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
+                    Name = table.Column<string>(type: "character varying(50)", nullable: false)
+                },
+                constraints: table =>
+                {
+                    table.PrimaryKey("PK_Tenants", x => x.Id);
+                });
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Users_TenantId",
+                table: "Users",
+                column: "TenantId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Users_Username_TenantId",
+                table: "Users",
+                columns: new[] { "Username", "TenantId" },
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Students_TenantId",
+                table: "Students",
+                column: "TenantId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Students_IdentityCard_TenantId",
+                table: "Students",
+                columns: new[] { "IdentityCard", "TenantId" },
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Professors_TenantId",
+                table: "Professors",
+                column: "TenantId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Professors_IdentityCard_TenantId",
+                table: "Professors",
+                columns: new[] { "IdentityCard", "TenantId" },
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Posts_TenantId",
+                table: "Posts",
+                column: "TenantId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Posts_ForumId_Title_TenantId",
+                table: "Posts",
+                columns: new[] { "ForumId", "Title", "TenantId" },
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_PostMessages_TenantId",
+                table: "PostMessages",
+                column: "TenantId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Forums_TenantId",
+                table: "Forums",
+                column: "TenantId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Forums_CourseId_Name_TenantId",
+                table: "Forums",
+                columns: new[] { "CourseId", "Name", "TenantId" },
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Courses_TenantId",
+                table: "Courses",
+                column: "TenantId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Courses_Name_TenantId",
+                table: "Courses",
+                columns: new[] { "Name", "TenantId" },
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Tenants_Name",
+                table: "Tenants",
+                column: "Name",
+                unique: true);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Courses_Tenants_TenantId",
+                table: "Courses",
+                column: "TenantId",
+                principalTable: "Tenants",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Forums_Tenants_TenantId",
+                table: "Forums",
+                column: "TenantId",
+                principalTable: "Tenants",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_PostMessages_Tenants_TenantId",
+                table: "PostMessages",
+                column: "TenantId",
+                principalTable: "Tenants",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Posts_Tenants_TenantId",
+                table: "Posts",
+                column: "TenantId",
+                principalTable: "Tenants",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Professors_Tenants_TenantId",
+                table: "Professors",
+                column: "TenantId",
+                principalTable: "Tenants",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Students_Tenants_TenantId",
+                table: "Students",
+                column: "TenantId",
+                principalTable: "Tenants",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Users_Tenants_TenantId",
+                table: "Users",
+                column: "TenantId",
+                principalTable: "Tenants",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+        }
+
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropForeignKey(
+                name: "FK_Courses_Tenants_TenantId",
+                table: "Courses");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Forums_Tenants_TenantId",
+                table: "Forums");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_PostMessages_Tenants_TenantId",
+                table: "PostMessages");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Posts_Tenants_TenantId",
+                table: "Posts");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Professors_Tenants_TenantId",
+                table: "Professors");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Students_Tenants_TenantId",
+                table: "Students");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Users_Tenants_TenantId",
+                table: "Users");
+
+            migrationBuilder.DropTable(
+                name: "Tenants");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Users_TenantId",
+                table: "Users");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Users_Username_TenantId",
+                table: "Users");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Students_TenantId",
+                table: "Students");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Students_IdentityCard_TenantId",
+                table: "Students");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Professors_TenantId",
+                table: "Professors");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Professors_IdentityCard_TenantId",
+                table: "Professors");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Posts_TenantId",
+                table: "Posts");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Posts_ForumId_Title_TenantId",
+                table: "Posts");
+
+            migrationBuilder.DropIndex(
+                name: "IX_PostMessages_TenantId",
+                table: "PostMessages");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Forums_TenantId",
+                table: "Forums");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Forums_CourseId_Name_TenantId",
+                table: "Forums");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Courses_TenantId",
+                table: "Courses");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Courses_Name_TenantId",
+                table: "Courses");
+
+            migrationBuilder.DropColumn(
+                name: "TenantId",
+                table: "Users");
+
+            migrationBuilder.DropColumn(
+                name: "TenantId",
+                table: "Students");
+
+            migrationBuilder.DropColumn(
+                name: "TenantId",
+                table: "Professors");
+
+            migrationBuilder.DropColumn(
+                name: "TenantId",
+                table: "Posts");
+
+            migrationBuilder.DropColumn(
+                name: "TenantId",
+                table: "PostMessages");
+
+            migrationBuilder.DropColumn(
+                name: "TenantId",
+                table: "Forums");
+
+            migrationBuilder.DropColumn(
+                name: "TenantId",
+                table: "Courses");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Users_Username",
+                table: "Users",
+                column: "Username",
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Students_IdentityCard",
+                table: "Students",
+                column: "IdentityCard",
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Professors_IdentityCard",
+                table: "Professors",
+                column: "IdentityCard",
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Posts_ForumId_Title",
+                table: "Posts",
+                columns: new[] { "ForumId", "Title" },
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Forums_CourseId_Name",
+                table: "Forums",
+                columns: new[] { "CourseId", "Name" },
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Courses_Name",
+                table: "Courses",
+                column: "Name",
+                unique: true);
+        }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs
index 993f66e..87302ae 100644
--- a/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs
@@ -30,9 +30,14 @@ namespace Tsi1.DataLayer.Migrations
                         .IsRequired()
                         .HasColumnType("character varying(50)");
 
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
                     b.HasKey("Id");
 
-                    b.HasIndex("Name")
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("Name", "TenantId")
                         .IsUnique();
 
                     b.ToTable("Courses");
@@ -52,9 +57,14 @@ namespace Tsi1.DataLayer.Migrations
                         .IsRequired()
                         .HasColumnType("character varying(50)");
 
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
                     b.HasKey("Id");
 
-                    b.HasIndex("CourseId", "Name")
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("CourseId", "Name", "TenantId")
                         .IsUnique();
 
                     b.ToTable("Forums");
@@ -88,6 +98,9 @@ namespace Tsi1.DataLayer.Migrations
                     b.Property<int>("ForumId")
                         .HasColumnType("integer");
 
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
                     b.Property<string>("Title")
                         .IsRequired()
                         .HasColumnType("character varying(100)");
@@ -97,9 +110,11 @@ namespace Tsi1.DataLayer.Migrations
 
                     b.HasKey("Id");
 
+                    b.HasIndex("TenantId");
+
                     b.HasIndex("UserId");
 
-                    b.HasIndex("ForumId", "Title")
+                    b.HasIndex("ForumId", "Title", "TenantId")
                         .IsUnique();
 
                     b.ToTable("Posts");
@@ -122,6 +137,9 @@ namespace Tsi1.DataLayer.Migrations
                     b.Property<int>("PostId")
                         .HasColumnType("integer");
 
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
                     b.Property<int>("UserId")
                         .HasColumnType("integer");
 
@@ -129,6 +147,8 @@ namespace Tsi1.DataLayer.Migrations
 
                     b.HasIndex("PostId");
 
+                    b.HasIndex("TenantId");
+
                     b.HasIndex("UserId");
 
                     b.ToTable("PostMessages");
@@ -145,9 +165,14 @@ namespace Tsi1.DataLayer.Migrations
                         .IsRequired()
                         .HasColumnType("character varying(50)");
 
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
                     b.HasKey("Id");
 
-                    b.HasIndex("IdentityCard")
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("IdentityCard", "TenantId")
                         .IsUnique();
 
                     b.ToTable("Professors");
@@ -182,9 +207,14 @@ namespace Tsi1.DataLayer.Migrations
                         .IsRequired()
                         .HasColumnType("character varying(50)");
 
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
                     b.HasKey("Id");
 
-                    b.HasIndex("IdentityCard")
+                    b.HasIndex("TenantId");
+
+                    b.HasIndex("IdentityCard", "TenantId")
                         .IsUnique();
 
                     b.ToTable("Students");
@@ -205,6 +235,25 @@ namespace Tsi1.DataLayer.Migrations
                     b.ToTable("StudentCourses");
                 });
 
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Tenant", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("integer")
+                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("character varying(50)");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("Name")
+                        .IsUnique();
+
+                    b.ToTable("Tenants");
+                });
+
             modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b =>
                 {
                     b.Property<int>("Id")
@@ -234,6 +283,9 @@ namespace Tsi1.DataLayer.Migrations
                     b.Property<int?>("StudentId")
                         .HasColumnType("integer");
 
+                    b.Property<int>("TenantId")
+                        .HasColumnType("integer");
+
                     b.Property<int>("UserTypeId")
                         .HasColumnType("integer");
 
@@ -249,9 +301,11 @@ namespace Tsi1.DataLayer.Migrations
                     b.HasIndex("StudentId")
                         .IsUnique();
 
+                    b.HasIndex("TenantId");
+
                     b.HasIndex("UserTypeId");
 
-                    b.HasIndex("Username")
+                    b.HasIndex("Username", "TenantId")
                         .IsUnique();
 
                     b.ToTable("Users");
@@ -276,6 +330,15 @@ namespace Tsi1.DataLayer.Migrations
                     b.ToTable("UserTypes");
                 });
 
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Courses")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
             modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
                 {
                     b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
@@ -283,6 +346,12 @@ namespace Tsi1.DataLayer.Migrations
                         .HasForeignKey("CourseId")
                         .OnDelete(DeleteBehavior.Cascade)
                         .IsRequired();
+
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Forums")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
                 });
 
             modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b =>
@@ -308,6 +377,12 @@ namespace Tsi1.DataLayer.Migrations
                         .OnDelete(DeleteBehavior.Cascade)
                         .IsRequired();
 
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Posts")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
                     b.HasOne("Tsi1.DataLayer.Entities.User", "User")
                         .WithMany("Posts")
                         .HasForeignKey("UserId")
@@ -323,6 +398,12 @@ namespace Tsi1.DataLayer.Migrations
                         .OnDelete(DeleteBehavior.Cascade)
                         .IsRequired();
 
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("PostMessages")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
                     b.HasOne("Tsi1.DataLayer.Entities.User", "User")
                         .WithMany("PostMessages")
                         .HasForeignKey("UserId")
@@ -330,6 +411,15 @@ namespace Tsi1.DataLayer.Migrations
                         .IsRequired();
                 });
 
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Professors")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
             modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b =>
                 {
                     b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
@@ -345,6 +435,15 @@ namespace Tsi1.DataLayer.Migrations
                         .IsRequired();
                 });
 
+            modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b =>
+                {
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Students")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
             modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b =>
                 {
                     b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
@@ -370,6 +469,12 @@ namespace Tsi1.DataLayer.Migrations
                         .WithOne("User")
                         .HasForeignKey("Tsi1.DataLayer.Entities.User", "StudentId");
 
+                    b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
+                        .WithMany("Users")
+                        .HasForeignKey("TenantId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
                     b.HasOne("Tsi1.DataLayer.Entities.UserType", "UserType")
                         .WithMany()
                         .HasForeignKey("UserTypeId")
-- 
GitLab