Last post Dec 01, 2017 02:52 AM by Edward Z
Nov 30, 2017 07:34 AM|Ashvini2004|LINK
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
public partial class PACEntities : DbContext
public PACEntities(string 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
public class PACIdentityDbContext : IdentityDbContext<ApplicationUser>
public PACIdentityDbContext(DbContextOptions<PACIdentityDbContext> options)
"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'"
var builder = new EntityConnectionStringBuilder(Configuration.GetConnectionString("PACEntities"));
var regularConnectionString = builder.ProviderConnectionString;
// 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;
// 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;
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>();
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)
Nov 30, 2017 05:21 PM|PatriceSc|LINK
As you wish. I would keep separate contexts unless you do see some actual benefit about merging them. You can always "link" them by providing an id you get from ASP.NET identity as a criteria to a query done using your business context.
Even with EDMX you should be able to create a partial class and use the model builder to ignore the ASP.NET identity tables. Would have to try.
The last exception seems unrelated to the previous discussion. It seems it would be better to discuss each issue in its own thread. Seeing the Account and FileType entities might help.
Dec 01, 2017 02:52 AM|Edward Z|LINK
As the suggestion from PatriceSc, it would be better to discuss each issue in separate thread.
>> 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?
For this issue, could you share us how you update edmx file? As
Porting an EF6 EDMX-Based Model to EF Core points,
EF Core does not support the EDMX file format for models. The best option to port these models, is to generate a new code-based model from the database for your application.
You could refer link below for generate model from existing database.
# Getting Started with EF Core on ASP.NET Core with an Existing Database
In addition, do you need Identity table? if not, you could try DbContext instead of IdentityDbContext.