Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  2. using Microsoft.EntityFrameworkCore;
  3. using Microsoft.Extensions.Configuration;
  4.  
  5. using ProbeSeeker.Models;
  6. using System.IO;
  7.  
  8. namespace DatabaseConnection.Models
  9. {
  10.     public class DataContext : IdentityDbContext
  11.     {
  12.         public DataContext() :
  13.             base()
  14.         { }
  15.  
  16.         public DataContext(DbContextOptions<DataContext> options) :
  17.             base(options)
  18.         { }
  19.  
  20.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  21.         {
  22.             var builder = new ConfigurationBuilder()
  23.                                     .SetBasePath(Directory.GetCurrentDirectory())
  24.                                     .AddJsonFile("appsettings.json");
  25.             var configuration = builder.Build();
  26.             optionsBuilder.UseSqlServer(configuration["ConnectionStrings:DefaultConnection"]);
  27.         }
  28.  
  29.         protected override void OnModelCreating(ModelBuilder modelBuilder)
  30.         {
  31.             base.OnModelCreating(modelBuilder);
  32.  
  33.             modelBuilder.Entity<UserRoleModel>()
  34.                 .HasKey(ur => new { ur.UserId, ur.RoleId });
  35.             modelBuilder.Entity<UserRoleModel>()
  36.                 .HasOne(ur => ur.User)
  37.                 .WithMany(u => u.UserRoles)
  38.                 .HasForeignKey(r => r.UserId);
  39.             modelBuilder.Entity<UserRoleModel>()
  40.                 .HasOne(ur => ur.Role)
  41.                 .WithMany(r => r.UserRoles)
  42.                 .HasForeignKey(u => u.RoleId);
  43.         }
  44.  
  45.         public DbSet<RoleModel> MyRoles { get; set; }
  46.         public DbSet<UserRoleModel> MyUserRoles { get; set; }
  47.         public DbSet<FacilityModel> Facilities { get; set; }
  48.         public DbSet<SpecializationModel> Specializations { get; set; }
  49.         public DbSet<TestModel> TestModels { get; set; }
  50.  
  51.     }
  52.  
  53.     public class TestModel
  54.     {
  55.         public int Id { get; set; }
  56.  
  57.         public int TestData { get; set; }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement