Hi
We have existing MVC 4 application with Entityframework 4.0 so we are using database first approach for that.
The dbcontext structure is as below
using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class PACEntities : DbContext { public PACEntities(string connectionString) : base(connectionString) { } }
And we are initialising dbcontext object by calling following function
public static PACEntities Create() { return new PACEntities(ConnectionString); }
Now we have converted our application to asp.net core 2.0 and want to implement Asp.net identity in that.Currently we have seperate DAL layer class library where all database related classes and DBContext are defined.We are going to use same database for identity also, so should we use same DBContext or different for identity because there are some other existing tables also which are related to identity tables.So if we use same dbcontext then facing some problems as below,
- After updating edmx file now it'll display identity table DBsets also in DBContext file like AspNetUser,AspNetUserRole etc.So how that should be used or how to customise those only?
- Second problem is now the DBContext will be as below
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Identity; namespace GlassBox.PAC.DAL { public class PACIdentityDbContext : IdentityDbContext<ApplicationUser> { public PACIdentityDbContext(DbContextOptions<PACIdentityDbContext> options) : base(options) { } } }
Connectionstring
"ConnectionStrings": {"PACEntities": "metadata=res://*/PAC.csdl|res://*/PAC.ssdl|res://*/PAC.msl;provider=System.Data.SqlClient;provider connection string='Data Source=10.35.0.45;Initial Catalog=PAC_dev;Integrated Security=False;user id=cdsnet;password=cdsn3tpa$$;Pooling=False;MultipleActiveResultSets=True'" },
ConfigureServices code
var builder = new EntityConnectionStringBuilder(Configuration.GetConnectionString("PACEntities")); var regularConnectionString = builder.ProviderConnectionString;services.AddDbContext<PACEntities>(options => options.UseSqlServer(regularConnectionString)); services.AddIdentity<DAL.ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<PACEntities>() .AddDefaultTokenProviders(); services.Configure<IdentityOptions>(options => { // Password settings options.Password.RequireDigit = true; options.Password.RequiredLength = 8; options.Password.RequireNonAlphanumeric = false; options.Password.RequireUppercase = true; options.Password.RequireLowercase = false; options.Password.RequiredUniqueChars = 6; // Lockout settings options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); options.Lockout.MaxFailedAccessAttempts = 10; options.Lockout.AllowedForNewUsers = true; // User settings options.User.RequireUniqueEmail = true; }); services.ConfigureApplicationCookie(options => { // Cookie settings options.Cookie.HttpOnly = true; options.Cookie.Expiration = TimeSpan.FromDays(150); options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied options.SlidingExpiration = true; }); ContextFactory.ConnectionString = Configuration.GetConnectionString("PACEntities");
Create method for initialising DBContext and 'Connectionstring' is defined in statup.cs file as highlighted above
public static PACEntities Create()
{
var options = new DbContextOptionsBuilder<PACEntities>();
options.UseSqlServer(ConnectionString);
return new PACEntities(options.Options);
}
Do we need this method but if we dont use above code then DBContext variable is null.So using all above code when i run my application i am getting below exception,(Here Account,FileType are our exisitng table and field)
InvalidOperationException: Unable to determine the relationship represented by navigation property 'Account.FileType' of type 'ICollection<FileType>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
I have gone through some blogs but not getting how to implement this exactly .Can you please explain and provide one sample code where it's defined complete flow of how we can use exisitng db model with new identity dbcontext.
https://stackoverflow.com/questions/19764233/merge-mydbcontext-with-identitydbcontext(Gone through this link but not understood the solution clearly)
Thanks,
Ashvini Awaskar