Search code examples
c#sql-servermodelasp.net-mvc-5datacontext

Can we create DbContext with two models


I'm new to Asp.net and created a web application using MVC5 in Asp.Net. And I have a quick question like can we create DbContext with two models(As shown in below image).

enter image description here

From above code, I can able to apply CRUD operations to Employee model.. But I'm getting this exception:

System.Data.SqlClient.SqlException: Invalid object name 'dbo.User Details'

For UserDetails model, and I'm suspecting that it is happening only because of two models in one DbContext.. Is my approach is correct or not?

please help me..


Solution

  • It may be to do with pluralizing database names. In your EmployeeContext class try

            protected override void OnModelCreating(DbModelBuilder modelBuilder)  {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            }
    

    Example:

    enter image description here