简体   繁体   中英

ASP .NET MVC Database First Individual User Accounts

I am developing a large MVC Web Application with ASP .NET. I make it as Database First, not Code First.

By creating the Web App with Individual User Accounts Authentication it creates the models, views, controllers and the database for it, which is pretty nice

But I want to change the migration to use my hosted database in the cloud. I tried to change [base("DefaultConnection",...)] in the IdentityModel.cs to use the hosted database, and I also tried to make changes on the Web.config

It was not a good idea! :D

Please, help me!

You just need to search for the "DefaultConnection" ConnectionString in your Web.config file, replace it with your remote server's information and that should be enough to connect to your hosted database.

It should look like this:

<add name="DefaultConnection" connectionString="data source=**YourServer**;initial catalog=**YourDatabaseName**;user id=**DatabaseUsername**;password=**DatabasePassword**" providerName="System.Data.SqlClient" />

I working on a project like you do. I'm using Database First Approach. After create a project with Individual User Authentication, you need to transfer these user tables to your database. You can do this with schema compare.

On the SQL Server Object Explorer > find your localdb that contain user tables > right click > Schema Compare

schema compare

Then select target (your database) and click Compare on right corner.

Select table(s) you want to transfer to your database.

After doing this edit Web.config file:

<add name="DefaultConnection" connectionString="Data Source=YourServerName;Initial Catalog=YourDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />

(This connectionString is to use default model, controllers and actions provided by project. If you want to create your own functions for user operations you only need to add below line after transfer tables.)

Here, provider name is important. Don't chance it. This connection string is for user operations.

Then add ADO.NET Entity Data Model to use your other tables with Entity framework

<add name="YourEntityName" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=YourServerNameL;initial catalog=YourDBName;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

providerName is important here as well.

  1. I add my ado.net in (model folder)

  2. on the web.config change my new connection string to "DefaultConnection"

  3. in the Dbcontext "my ado.net" change to : base("DefaultConnection")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM