I'm using DbUp to migrate my SQL Server database. I've implemented this in a console application, that accepts the connection string. If I run this against my local SQL Server database, it works. If I run it against an Azure SQL database, it works as well.
However, if I run it against a LocalDb database that was created on the fly in my tests, I get an exception.
The line on which the exception occurs is:
EnsureDatabase.For.SqlDatabase(connectionString);
and the connection string parameters contains:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDBFileName=C:\Projects\Prim\mgp\Prim.Mgp.Infrastructure.DataAccessTests\bin\Debug\netcoreapp2.2\Data\ccf243baa63eb4ea258b0155.mdf;Initial Catalog=ccf243baa63eb4ea258b0155;Integrated Security=True;MultipleActiveResultSets=True;
The exception is:
System.Data.SqlClient.SqlException: 'Database 'C:\Users\ludwigs\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB\master.mdf' already exists. Choose a different database name. Cannot attach the file 'C:\Projects\Prim\mgp\Prim.Mgp.Infrastructure.DataAccessTests\bin\Debug\netcoreapp2.2\Data\ccf243baa63eb4ea258b0155.mdf' as database 'master'.'
...which is a bit strange, because it mentioning a different database than I specified in the connection string.
Does anyone have a clue what I might be doing wrong?
PS: if I remove the troublesome line of code , I can query my database without problems.
There is a problem with the AttachDBFileName in the connection string, try to test with this connection string.
And DBUp run the master so it can create the database using Ensure Database.For.Sql Database,
<connectionStrings>
<add name="Database" connectionString="Server=.;Database=Migrations_DbUp;Integrated Security=true"/>
</connectionStrings>
Related
I have followed the example from Microsoft documentation for a simple code first approach on a .NET Framework database generation. I checked my data source and confirmed it works as when I go to add a new data connection via Microsoft SQL Server the name of my configuration File shows up under the drop down for available tables.
When I test the connection to my table I get a successfully response.
When I click to add the new data connection I get the following error:
Previously I was able to connect to some other local databases like Northwind and other simple test databases, but not when ever I try to connect to any db I get this error. I tried in a VM as well and still get the same error.
Any suggestions?
EDIT: connection string:
connectionString="data source=(LocalDb)\MSSQLLocalDB;initial catalog=Proj.TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
EDIT 2: When running a query in SQL Server Management Studio, I am able to see the generated tables and can even see how the code populated them (I had a simple add statement as a test). But for some reason I can not add it as a db connection in Visual Studio 2019
I have worked with EF for a while now and i have always used LocalDb's for storing data. I want to start working with SQL Server databases instead but I'm having some issues setting up the connection string.
https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx#sse
https://www.connectionstrings.com/
and looked over google but none of the answers made it work in my case so I must be doing something wrong (some of the connections strings throw an exception others didn't but wouldn't insert anything either into the database neither)
My question is when working with EF & SQL Server, should I use both the connection string in the App.config & setting the path of the DB in the CTOR of the context (by using AppDomain.CurrentDomain.SetData("DataDirectory", path);) or is the app.config sufficient ?
I have tried the following connection strings:
Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;Trusted_Connection=True;MultipleActiveResultSets=True;
Data Source=.\GURUBEAST-PC\GURUSQL;Database=iManager;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=True;
Data Source=.\GURUBEAST-PC\GURUSQL;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.GURUSQL\MSSQL\DATA\iManager.mdf;Database=iManager;Trusted_Connection=True;MultipleActiveResultSets=True;
Data Source=.\GURUBEAST-PC\GURUSQL;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.GURUSQL\MSSQL\DATA\iManager.mdf;Database=iManager;Trusted_Connection=True;
Data Source=.\GURUBEAST-PC\GURUSQL;Database=iManager;Trusted_Connection=True;
Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;Integrated Security=SSPI;
Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;User id=GURUBEAST-PC\GuruBeast;
Where "iManager" is the name of the database. I use Windows auth for my SQL Server instance.
What am I doing wrong ? Should I set my path to the program files folder or the App_Data (I have seen both and tried both but both didn't work)?
Kind regards!
The Data Source key is used to find the machine on which the Sql Server instance runs.
You can have different strings for it but the most common used in a LAN environment is composed using the name of the server machine followed by an eventual instance name.
So, if your local PC is named GURUBEAST-PC and, at install time, you haven't specified any instance name, the connectionstring Data Source contains only the name of the machine GURUBEAST-PC. If you have an instance name then you should add that instance name to you Data Source key. GURUBEAST-PC\GURUSQL
This will guarantee to all the PC in the same LAN the possibility to have the same connectionstring also if the connection is made from the same PC where the SQL Server runs.
If the Data Source points at the local pc, you can use many shortcuts to represent the local PC:
(LOCAL)
localhost
.
\.
and eventually add the instance name to these shortcuts without repeating the PC name
Once you get your host name figured out, Entity Framework will generate your connection string for you. Here's a sample of what your connection string could look like if you were attempting to connect to AdventureWorks database hosted on your local instance of SQL Server 2014 aptly named sql2014.
<connectionStrings>
<add name="AdventureWorksEntities" connectionString="metadata=res://*/DataModels.AdventureWorksDb.csdl|res://*/DataModels.AdventureWorksDb.ssdl|res://*/DataModels.AdventureWorksDb.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sql2014;initial catalog=AdventureWorks;persist security info=True;user id=App_AdventureWorks;password=asdasdfasdfasdf;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Your db context would then look something like this.Again, EF generates this for you.
public partial class AdventureWorksEntities : DbContext
{
public AdventureWorksEntities()
: base("name=AdventureWorksEntities")
{
}
I have created an application using Entity Framework 6 code-first, in ASP.NET MVC 5, and am bin deploying it to my server. Everything works fine, except for the operations/controller actions that involve database usage.
I am uploading the once-generated database file from my computer to the App_Data folder of the server.
Upon deploying, I changed the connection string in my web.config file from:
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-NERC_Main-20160104065223.mdf;Initial Catalog=aspnet-NERC_Main-20160104065223;Integrated Security=True"
to
connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\aspnet-NERC_Main-20160104065223.mdf;Initial Catalog=aspnet-NERC_Main-20160104065223;Database=aspnet-NERC_Main-20160104065223.mdf;Trusted_Connection=Yes;Integrated Security=True;"
which throws an error of
CREATE DATABASE permission denied in database 'master'.
A probable cause of this is because the present database file is not being attached, and the entity framework is trying to generate a new database in some other, restricted directory.
I read that the AttachDbFileName is valid for SQL Server Express instances only, which in my case doesn't exist.
How can I modify the connection string so that my current, already uploaded database is utilized.
Note:
The remote server has a full installation of SQL Server 2008 R2. The server does not support user instances. My database is not password protected, and hence I've set the Integrated Security property value to True. I'll provide any other information if required.
If you have a full version of SQL Server, you cannot just use AttachDbFileName. Instead, you need to copy the .mdf (and .ldf) to the SQL Server data file location and then you need to attach the database in SQL Server Management Studio to the server instance.
From that point on, you can reference that database in your connection string using the server name, and the logical database name - something like this:
Server=.;Database=aspnet-NERC_Main-20160104065223;Integrated Security=True;
Depending on your database setup, you may or may not be able to use the Integrated Security - maybe you'll need to have a specific SQL Server login and specify that login (and its password) in your connection string instead:
Server=.;Database=aspnet-NERC_Main-20160104065223;User ID=YourUserName;Password=YourPassword
See this site here for a ton of sample of how to build valid connection strings for SQL Server.
I am trying to avoid using my root user as the user that accesses my MySQL database in my ASP.NET web app. Here is my connection string:
<add name="MyConnectionString" providerName="MySql.Data.MySqlClient" connectionString="Data Source=90.232.111.231;Initial Catalog=mydb;User ID=root;Password=rootpassword;Convert Zero Datetime=True"/>
The application works with no issue with that connection string. However, when I change the connection string to this:
<add name="MyConnectionString" providerName="MySql.Data.MySqlClient" connectionString="Data Source=90.232.111.231;Initial Catalog=mydb;User ID=myuser;Password=mypassword;Convert Zero Datetime=True"/>
I get the following exception:
Parameter 'eturnValue' not found in the collection.
Exception Details: System.ArgumentException: Parameter 'eturnValue' not found in the collection.
This comes during the log in at the first attempt to access the database.
I have no idea where eturnValue is coming from, as the only difference between the runs is which user I am try to access the database with. I confirmed that my mysql user has been set up correctly, it has all the privileges the root account does, at least that is what I was told by my host's support.
I am calling a stored procedure through the MySql objects in C#.
Something is trying to iterate through the items in a collection (presumably it would be looking for 'ReturnValue'), but somewhere it has been typed as 'eturnValue', and so cannot find it.
As I can't tell from your exception detail where it came from, you should perform a 'Find in Files' for 'eturnValue' - you may find that there is a typo in your code. Or check the Stored Procedures in the database for the same.
Slightly at my wits end. Built an application, running fine locally. Migrated my database to MSSQL without issue, uploaded the site, can't seem to get the application to connect to the database. Any page that accesses the database I get a generic error message.
I've tried all the separate combinations of connection strings I could think of using the Godaddy recommended connection strings. Perhaps I am overlooking something simple?
I'm using Entity Framework Code-First -- My context model is called CombosContext.
<add name="CombosContext" connectionString=" Server=jelatin.db.9508732.hostedresource.com; Database=jelatin; User ID=jelatin; Password=********; Trusted_Connection=False" providerName="System.Data.SqlClient" />
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString=" Server=jelatin.db.9508732.hostedresource.com; Database=jelatin; User ID=jelatin; Password=********; Trusted_Connection=False" providerName="System.Data.SqlClient" />
Server: jelatin.db.9508732.hostedresource.com
DB name: jelatin
user: jelatin
Table: Comboes
I'm unfamiliar with GoDaddy hosted SQL, but usually a connection string to MS SQL Server uses "Data Source" instead of "Server" and "Initial Catalog" instead of "Database".
UPDATE
I didn't realize Server and Database were allowed options in the Connection String. Sorry for the confusion.
Regarding the database itself - are you letting EF create the database? Does the user have permission on GoDaddy's system to create a database?
If you have already created the database, did you populate anything? I have found that EF Code First won't correctly populate the database if the database exists and the metadata table doesn't. If you can, try copying your local database up to GoDaddy, and see if the connection works.
Finally, for your generic error message - is it coming back in a 500 error? If so, have you tried using either IE or Chrome's dev tools to inspect the response? Better error information is usually hidden in there.