Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect ASP.net Identity 2.0 to an already existed database

i'm using vs2013 and using asp.net mvc 5 to create my web application. in order to make individual authentication , i'm using Identity 2.0 .
when i created my Entitymodel.edmx , it automatically generated new connection string to my db in web.config like here :

<add name="myEntities" connectionString="metadata=res://*/Models.mEntity.csdl|res://*/Models.mEntity.ssdl|res://*/Models.mEntity.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Shop.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

i suppose Identity uses code first approach to mak data base and is connected to another connection string (DefaultConnection) like :

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-testauthentication-20151116011628.mdf;Initial Catalog=aspnet-testauthentication-20151116011628;Integrated Security=True"
      providerName="System.Data.SqlClient" />

so as result when i first run project it creates new .mdf file with name of

aspnet-testauthentication-20151116011628.mdf

and create tables in it. but what i want is to make Identity connect to my already created db named : Shop.mdf and create it's table in it. so i put my Connection string name in ApplicationDbContext class in IdentityModel.cs like here :

public ApplicationDbContext()
        : base("myEntities", throwIfV1Schema: false)
{
}

but i get following error :

The entity type ApplicationUser is not part of the model for the current context.

so what is the appropriate and correct way to doing that ?

thanks a lot

UPDATE :
i created Identity's table exactly with the same name in my existing data base and the DefaultConnection to connect to existing database and now i getting this error :

Cannot create file 'c:\users\mohamad\documents\visual studio 2013\Projects\Shop\Shop\App_Data\Shop.mdf' because it already exists. Change the file path or the file name, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created.

why when there is a database and even table in it, identity tries to create data base ??

Solution :
point 1 : it seems identity doesn't have problem with Connecting to an already existing database, it can nicely Connects to it and creates it's table on it.

point 2 : the problem i had : i don't know what was the cause of my problem but i did following steps and problem resolved :

  1. delete all table from model.edmx
  2. delete model.edmx
  3. regenerate edmx model and Connection string as well, then i added tables to it
  4. DefalutConnection string points to exactly same .mdf file which Entity Connection string do.
  5. run project and register and every thing goes well.
  6. initial catalog in connection string should be filled

if you need to add some another property in addition to existing property of Identity like name,City and .... do following step and the run the project and register :

  1. in IdentityModel.cs file add properties into ApplicationUser class:

    public class ApplicationUser : IdentityUser { public string Adress { get; set; } public DateTime DateOfBirth { get; set; } .... }

  2. in AccountViewModel.cs in RegisterViewModelclass add exactly the same property and assign their attribute like [Requierd]
  3. in AccountController.cs in Register action add properties like this :

    var user = new ApplicationUser() { UserName = model.Email, Email = model.Email ,Adress=model.Adress};

  4. add related html to Register view

cheers

like image 792
M.Armoun Avatar asked Nov 10 '22 01:11

M.Armoun


1 Answers

You have two choices. You can either have Identity occupy a separate database that's set up using Code First and then use your existing database via a second context or you can simply create the tables Identity needs in your existing database, manually. You won't be able to auto-generate them.

With the first option, you will not be able to create relationships between any of the Identity entities and entities originating from your existing database, i.e. no navigation properties and no foreign keys. You could still store the user id in a standard column and then manually use that to look up the appropriate user out of the Identity context, though.

With the second option, you'd just need to know what tables need to be created. For that, you can allow the tables to be generated via Code First, then simply move the schema over to your existing database before removing the Identity-specific context and updating your EDMX.

Also, for what it's worth, you should avoid using EDMX at this point. It has been deprecated and all support for it will be removed from EF7. The EF team has pledged to continue to support EF6 with security patches and bug fixes for the time being, but you're still merely delaying the inevitable. Despite the seemingly contradictory name, Code First can be used with an existing database, and that is definitely the preferred method for working with existing databases with EF.

like image 144
Chris Pratt Avatar answered Nov 14 '22 22:11

Chris Pratt