Im working on a MVC Application and so far ive been using localDB since that was which was included in the tutorial. I want to switch the application now to my SQL Server but im not sure how to.
I get that I have to change the connection String. But not in what way exactly since the SQL Server has a username and password which my localDB doesnt.
Another question regarding this is, do I have to create the tables myself in the beginning on the SQL Server or will they be generated by the entity framework like in my localDB?
Current connection string for localDB:
<add name="AcquisitionDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Acquisitions.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
Im pretty new to this so any help is very much appreciated
if you are going to use MS SQL then it wil look something like this:
<add name="AcquisitionDBContext" connectionString="Data Source=hostname\sql_instance_name;Initial Catalog=databaseName;Persist Security Info=True;User ID=username;Password=password;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Where "hostname\sql_instance_name" is the servername(or ip)/the sql instance name eg. localhost\sqlexpress
ConnectionStrings.com is the best reference for connection-string syntax. It shows you the options to use when you have a username+password. In this case:
Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;
Entity Framework will not create your tables for you unless you tell it to by calling something like this:
Database.SetInitializer( new DropCreateDatabaseIfModelChanges<YourDbContextHere>() );
(Where Database is the System.Data.Entity.Database type).
Try it:
<connectionStrings>
<add name="<<Name of Connection String>>" connectionString="Data Source=<<Put here path to your SQL SErver>>;Initial Catalog=<<Database name>> ;Integrated Security=SSPI;Trusted_Connection=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
Here's how connection string should look like (tweak to your needs)
<add name="DefaultConnection" connectionString="Data Source=server_name,port; Initial Catalog=your_catalogue_name; Integrated Security=False; User ID=userName;Password=Passwd; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
Before first run I would recommend using migrations.
Go to PackageManagerConsole (if you don't have it in menu, install NuGet first via Extension and Updates)
Then inside console type Enable-Migrations (if you didn't already).
Next type Add-Migration your_migration_name and then Update-Database to let migrations create tables for you.
If you change something in your model files later, you can again type Add-Migration your_migration_name and Update-Database to update database with those changes.
Related
I have set up ASP.NET Identity with MySQL Provider according to this two guides: Asp.NET and Codeproject.
I use EntityFramework and ADO.NET to access to other tables in database and have one connection string for this. The other connection string (DefaultConnection) is for Identity.
I have two different databases - one for development and one for production. Firs is on my local computer, the other is on Amazone server.
This is how my web.config looks like:
<connectionStrings>
<add name="DefaultConnection" connectionString="Database=[DB_NAME];Data Source=localhost;User Id=root;Password=[DB_PASS]" providerName="MySql.Data.MySqlClient" />
<add name="AdoNetConnection" connectionString="metadata=res://*/HealthModel.csdl|res://*/HealthModel.ssdl|res://*/HealthModel.msl;provider=MySql.Data.MySqlClient;provider connection string="database=[DB_NAME];server=localhost;user id=root;password=[DB_PASS]"" providerName="System.Data.EntityClient" />
Web.Release.config
<connectionStrings>
<add name="DefaultConnection"
connectionString="Database=[DB_NAME];Data Source=[DB_SERVER];User Id=[DB_USER];Password=[DB_PASS]" providerName="MySql.Data.MySqlClient"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)"/>
<add name="AdoNetConnection"
connectionString="metadata=res://*/HealthModel.csdl|res://*/HealthModel.ssdl|res://*/HealthModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=[DB_SERVER];user id=[DB_USER];password=[DB_PASS];persistsecurityinfo=True;database=[DB_NAME];" providerName="System.Data.EntityClient"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)"/>
</connectionStrings>
When I build and run application in debug, everything is great. I can access to other data tables from database and also to identity data tables to register a new user. If tables don't exist, they are automatically created (migration).
But when I publish it to Azure in Release mode, there comes a problem. I can still access to the other data tables, but I can not register new user. Actually, everything goes ok when registering, but user does not show up in database - in table aspnetusers. Data tables for Identity, including aspnetusers are not even created, although they are not here.
As I said, I look in development database in debug mode and in release database in release mode.
Could please anyone help me to figure out, where is a problem? Thank you very much!
I am learning basics of MVC. And forgive me If I am not able to ask question properly because I am not aware of various technical terms. Anyways I am trying build one simple page where I will have two tables courses and Instructors. I am able to do most of the stuff and it seems to be working. Also I do have a solution given by my faculty of the same problem. When I am comparing my solution and faculty solution then in the web.config file of the actual solution given by my faculty I see something like
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcCRUDwithSQL-20140217025002.mdf;Initial Catalog=aspnet-MvcCRUDwithSQL-20140217025002;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="CourseContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=CourseContext-20140217145250; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|CourseContext-20140217145250.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
Where as in my solution that I just have one add name tag. I don't know why the another tag did not get created automatically. Or do I need to add that tag manually.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication9-20150313042831.mdf;Initial Catalog=aspnet-WebApplication9-20150313042831;Integrated Security=True" providerName="System.Data.SqlClient" />
Have I done something wrong that the connection string related to CourseContext did not get generated automatically. Or we need to add for CourseContext manually? Please help me. Since most of the files and its content are generated automatically I must have done something wrong that connection string pertaining to CourseContext did not get generated.
Use the steps below to generate the connection string via visual studio server explorer
Click on server explorer
Connect to database
Choose server name from dropdown
Choose your authentication type
Select your database from dropdown
Test connection
Copy the connection string from the properties and paste in your web.config
Also take a note of #Guffa answer, he's got a very good point
You don't need one connection for each table, you only need one connection for each database. A database can contain many tables (the project I'm working on right now has 9 tables, and that is a really small project).
The Visual Studio template starts out with a default connection string, but no database. It looks like it's that connection string that is still left in the solution.
I've created project to read from MSSQL DB and display some data. VS's wizards created connection string like the following:
<add name="SomeContext" connectionString="data source=xxx.xxx.xxx.xxx\_name,port;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
When I tried to deploy application to the real web server I changed connection string to use faceless DB account:
<add name="SomeContext" connectionString="data source=xxx.xxx.xxx.xxx\_name,port;User Id=userid;Password=password; MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
It looks like EF thinks that there some migration needed and tries to execute a lot of meaningless operations including attempts to execute SQL expressions with schema creating.
I have no write permissions on this DB server and I don't see any reasons to allow EF change DB structure.
So I'm completely lost there. What is the proper way to change authorization methods without changing on DB side?
Try to change connection string to production in VS project, then use Update-Database via Package Manager Console.
Pretty strange for me but the root of the issue was in default "initial catalog". It was stored somewhere for connection with integrated security but when I changed authorization method default was broken. As soon I defined initial catalog it was fixed.
This is my first MVC application so please be patient and kind. I have created an ASP.NET MVC 4 Web Application in Visual Studio 2012. I created the database code first, so all I did was create the model and a data context and a SQL server database was created for me. Every time I changed the model I could just update the database. I also connected to an Oracle database get other data that I needed. These are the connection strings that were used:
<connectionStrings>
<add name="ApplicationDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=something;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ConnectionString" connectionString="Data Source=something;Persist Security Info=True;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>
However, the database related to the model needs to be in Oracle (and I need a script for it but that is next weeks question) not SQL.
I was hoping that I could just change the data context provider name in the web config like so:
<connectionStrings>
<add name="ApplicationDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=something;Integrated Security=True" providerName="System.Data.OracleClient" />
<add name="ConnectionString" connectionString="Data Source=something;Persist Security Info=True;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>
But that is not working - it gives me errors.
Please note that I am NOT asking about OracleMembershipProvider. This question is unique in that I want the model to relate to an Oracle database. I am not worried about membership and user authentication etc. I have already taken care of that.
It seems like it should be simple enough, but I have googled until every link I find is purple.
I need you to tell me the steps and what to do (hopefully in the web config) to get my model to 'create' an Oracle database. Please.
After reading through a ton of articles I found out that Oracle doesn't support code first (yet) and although there are some work-a-rounds it’s probably best to create scripts to create the db for us.
So as an alternate solution we to created the database in Oracle first and then connected to it from the MVC application.
We added an ADO.NET Entity Data Mode and in the EDM Wizard we said "Generated from database" and added a new connection to the Oracle database. You need Oracle Developer Tools for Visual Studio for this.
Add a new controller and for the data context class, choose Entities. Don't forget to add the connection string to the web config.
<add name="Entities" connectionString="...;provider=Oracle.DataAccess.Client;provider connection string=...;data source=...;" providerName="System.Data.EntityClient" />
This should be done before you start coding because you no longer have a model so I have had to change my code quite a lot. Not more than a day or two's work though.
I have been trying to learn how to use sql express with C# and I am having difficulty connecting to a database that already has data in it. When I connect to the sql server my program adds a new database instead of reading the one that is currently in the sql server.
Here is my connection string which I think is where the issue is.
<add name="GolfLeague1" providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=aspnet-WebUI-20140205175325;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebUI-20140205175325.mdf" />
This gives me a new database called GolfLeague1(WebUI). Which I can read and write to, but it is not what I would like to do.
I have tried the string with and without a "Database=".
What part of the big picture am I missing here?
You have specified 'AttachDbFileName' property when you already have specified the database name. This part is not required here.
You can use the below mentioned connection string.
<add name="GolfLeague1" providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=aspnet-WebUI-20140205175325;Integrated Security=SSPI;" />