[英]ASP .NET MVC Database First Individual User Accounts
I am developing a large MVC Web Application with ASP .NET. 我正在使用ASP .NET开发大型MVC Web应用程序。 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 通过使用单个用户帐户身份验证创建Web应用,它可以为其创建模型,视图,控制器和数据库。
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
我试图在IdentityModel.cs
更改[base("DefaultConnection",...)]
以使用托管数据库,并且我还尝试在Web.config
上进行更改
It was not a good idea! 这不是一个好主意! :D :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. 您只需要在Web.config文件中搜索“ DefaultConnection” ConnectionString,将其替换为远程服务器的信息即可,这足以连接到托管数据库。
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
在SQL Server Object Explorer
>找到包含用户表的localdb>右键单击> Schema Compare
Then select target (your database) and click Compare
on right corner. 然后选择目标(您的数据库),然后单击右上角的Compare
。
Select table(s) you want to transfer to your database. 选择要转移到数据库的表。
After doing this edit Web.config
file: 完成此编辑Web.config
文件后:
<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.) (此connectionString用于使用项目提供的默认模型,控制器和操作。如果要为用户操作创建自己的函数,则只需在传输表之后添加以下行。)
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 然后添加ADO.NET实体数据模型以将其他表与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="data source=YourServerNameL;initial catalog=YourDBName;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
providerName
is important here as well. providerName
在这里也很重要。
I add my ado.net in (model folder) 我在(模型文件夹)中添加ado.net
on the web.config change my new connection string to "DefaultConnection" 在web.config上,将新的连接字符串更改为“ DefaultConnection”
in the Dbcontext "my ado.net" change to : base("DefaultConnection") 在Dbcontext“ my ado.net”中更改为:base(“ DefaultConnection”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.