Advertisement
AuriR

August 2015 - ElevenNoteDataContext.cs innards

Sep 1st, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using ElevenNote.Models;
  2. using Microsoft.AspNet.Identity.EntityFramework;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.Entity;
  6. using System.Data.Entity.ModelConfiguration;
  7. using System.Data.Entity.ModelConfiguration.Conventions;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace ElevenNote.DataAccess
  13. {
  14.     public class ElevenNoteDataContext : IdentityDbContext<ElevenNoteApplicationUser>, IDisposable
  15.     {
  16.         #region Constructor
  17.  
  18.         public ElevenNoteDataContext()
  19.             : base("DefaultConnection", throwIfV1Schema: false)
  20.         {
  21.             this.Configuration.LazyLoadingEnabled = false;
  22.         }
  23.  
  24.         public ElevenNoteDataContext(string connectionString = "DefaultConnection")
  25.             : base("DefaultConnection")
  26.         {
  27.             // Nothing else to do here; it's all handled by the base class.
  28.         }
  29.  
  30.         #endregion
  31.  
  32.         /// <summary>
  33.         /// Factory method to return a new instance.
  34.         /// </summary>
  35.         /// <returns></returns>
  36.         public static ElevenNoteDataContext Create()
  37.         {
  38.             return new ElevenNoteDataContext();
  39.         }
  40.  
  41.         #region Datasets
  42.  
  43.         // Below are the extra tables, aside from what's automatically created by the identity framework.
  44.  
  45.         public DbSet<Note> Notes { get; set; }
  46.  
  47.         #endregion
  48.  
  49.         #region Stuff to add because we're merging Identity Framework data creation with our own context
  50.  
  51.         protected override void OnModelCreating(DbModelBuilder modelBuilder)
  52.         {
  53.             modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  54.             modelBuilder.Configurations.Add(new IdentityUserLoginConfiguration()); // solves foreign key issues combining databases
  55.             modelBuilder.Configurations.Add(new IdentityUserRoleConfiguration());
  56.         }
  57.  
  58.         #endregion
  59.  
  60.     }
  61.  
  62.     public class IdentityUserLoginConfiguration : EntityTypeConfiguration<IdentityUserLogin>
  63.     {
  64.  
  65.         public IdentityUserLoginConfiguration()
  66.         {
  67.             HasKey(iul => iul.UserId);
  68.         }
  69.  
  70.     }
  71.  
  72.     public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
  73.     {
  74.  
  75.         public IdentityUserRoleConfiguration()
  76.         {
  77.             HasKey(iur => iur.RoleId);
  78.         }
  79.  
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement