2

I added a database to my project, then I want to add a controller.

When the Add Controller window pops up, I am asked to choose Data context class.

Surprisingly, I found that there are 2 context classes: One is called: my_database_name_dbEntities(projectname)

Another one is called:

ApplicationDbContext(projectname.Models) which is what I created when I added the Entity Framework object which connects to my database.

I am confused about

  1. which one to use
  2. what are the differences

Here is the screenshot enter image description here


Update

I tried both of them and here is what I have got:

  1. If I choose the databasename_dbEntities, VS generates views and controller perfectly with no problems.
  2. If I choose ApplicationDbContext, VS throws an error:

Error

There was an error running the selected code generator:

'Unable to retrieve metadata for 'lrc.Event'. One or more validation errors were detected during model generation:

AspNetUserLogin: : EntityType 'AspNetUserLogin' has no key defined. Define the key for this EntityType.

AspNetUserLogins: EntityType: EntitySet 'AspNetUserLogins' is based on type 'AspNetUserLogin' that has no keys defined.

'

enter image description here

Update

Now, I changed the super class from DbContext to IdentityDbContext for the projectname_dbEntities. So it looks like this now:

    public partial class projectname_dbEntities : IdentityDbContext<ApplicationUser>
{
        public projectname_dbEntities()
             : base("projectname_dbEntities", throwIfV1Schema: false)
            //: base("name=projectname_dbEntities")
        {
        }

        public static projectname_dbEntities Create()
        {
            return new projectname_dbEntities();
        }

.....
}

I wonder :

what are the advantages of using the the derived class from IdentityDbContext over the DbContext?

Franva
  • 6,565
  • 23
  • 79
  • 144
  • Use the one which corresponds to the tables you would like to access in the controller – bit Apr 06 '15 at 02:56
  • hi @bit they both correspond to my tables..... that's why I am confused... – Franva Apr 06 '15 at 02:57
  • possible duplicate of [ASP.NET Identity DbContext confusion](http://stackoverflow.com/questions/19902756/asp-net-identity-dbcontext-confusion) – bit Apr 06 '15 at 02:59
  • This one should clarify you : http://stackoverflow.com/questions/19308959/set-asp-net-identity-connectionstring-property-in-net-4-5-1 – bit Apr 06 '15 at 03:04
  • hi @bit I can see from the questions you found for me that the suggestion is to use ApplicationDbContext as it inherits from Identity utility. But when I chose it, it throws an error which prevents me to do anything. Please see my update. – Franva Apr 06 '15 at 03:16
  • hi @bit I think my problem is different from the one you refer to. – Franva Apr 06 '15 at 03:36

1 Answers1

1

Out of the box, when you create an ASP.NET MVC 5 project using the default template in Visual Studio 2013, you get a basic, ready-to-run website with the elementary Identity and Account management classes already in place.

There is a class ApplicationDbContext. This is the Entity Framework context used to manage interaction between your application and the database where your Account data is persisted (which may, or may not be the same database that will be used by the rest of our application) and this class inherits from IdentityDbContext.

IdentityDbContext is basically a regular DbContext with two DbSets. One for the Users and one for the Roles.

If you don't want use ASP.NET Identity, you can ignore it or mix it into your own DbContext class.

Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66
  • It clears me alot! So do you mean that I should use the projectname_dbEntities context class? – Franva Apr 06 '15 at 04:15
  • thanks. but if I use the projectname_dbEntities context class, then I will not have the access to the Identity facility class to use which is the latest built-in helper class. so I am confused why do they(MS) add this class if no one uses it? – Franva Apr 06 '15 at 04:40
  • 1
    You can keep both or mix them. if you want use `projectname_dbEntities`, this class should inherits from `IdentityDbContext` and add available `Dbsets` in `ApplicationDbContext` into `projectname_dbEntities`. – Mohsen Esmailpour Apr 06 '15 at 04:45
  • Thanks a lot~! Where can I find the method to mix them? – Franva Apr 06 '15 at 05:41