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.
Related
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 a C# windows form application which does many functions using the data from the database
Now I have a partner whose is working on the database part on his machine
He has database and i have all the forms and the application logic
How should i import his tables from his database engine ?
We both use SQL server 2008 R2 with database engines installed
I want that when i send my application to his machine (Say B) i should be able to use the database created by B to run the application of machine A(My machine ) ?
We are developing application in Visual studio 2010
Any help would be fantastic .
If it gets on a shared server, you can pull from different sources using different connection strings (using your database or his database, etc.). Here's a sample of connecting to 2 different database using 2 different connection strings in the Config file.
<connectionStrings>
<add name="Conn1" connectionString="Data Source=YourDataSource;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="Conn2" connectionString="Data Source=YourDataSource;uid=YourUserID;pwd=YourPassword;" providerName="System.Data.OracleClient" />
</connectionStrings>
Copying a SQL Server Database will be helpful also, because if a database is on his local machine and neither of you are connected via LAN or some intranet, you can't access it. It needs to be copied or put on a shared server/location where both of you can access it. Otherwise, see the link on how to copy the database.
Copying data from one database to another. This link should also help accomplish the meat of your question.
However, if you solely want "B" to be able to access it, and you don't care about the access, then just deploy your executable and change the connection string in his config file.
Also, see Vladimir's post regarding 'Backup'.
Easiest thing to do would be to run full backup on database in question. Below is sample of backup script.
USE YourDatabaseName;
GO
BACKUP DATABASE YourDatabaseName
TO DISK = 'C:\Backup\YourDatabaseName.Bak'
WITH FORMAT,
MEDIANAME = 'C_FullBackup',
NAME = 'Full Backup of YourDatabaseName';
GO
Documentaion on BACKUP http://technet.microsoft.com/en-us/library/ms187510.aspx#TsqlProcedure
Once backup is done copy the file over to your machine. Now you need to restore database from backup.
RESTORE DATABASE YourDatabaseName
FROM DISK = N'(path to your BAK file)'
WITH FILE = 1,
MOVE N'(your DB name)' TO N'(your SQL path)database.MDF',
MOVE N'(your DB name)_LOG' TO N'(your SQL path)database_LOG.LDF',
NOUNLOAD,
REPLACE,
STATS = 10
GO
Documenation on RESTORE http://technet.microsoft.com/en-us/library/ms186858.aspx
I have a C# winforms application, and a SQL Server .mdf database file.
I tried to use Entity Framework. It works great when retrieving data from the database, but on calling .SaveChanges() nothing happens. No error, no exception, and no changes saved.
This issue just drives me crazy. I found a bunch of questions with nearly the same issue, but neither of the answers apply to my case.
My code looks like that:
using (World_ParkingEntities context = new World_ParkingEntities())
{
client _client = context.CreateObject<client>();
_client.name = name;
_client.mobile_number = phoneNo;
_client.email = email;
context.AddToclients(_client);
context.SaveChanges();
}
My connection string:
<add name="World_ParkingEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\World_Parking.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /></connectionStrings>
The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!
If you want to stick with this approach, then try putting a breakpoint after the context.SaveChanges(); call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.
The real solution in my opinion would be to
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g. World_Parking)
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=.\\SQLEXPRESS;Database=World_Parking;Integrated Security=True
and everything else is exactly the same as before...
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.
I would like to be able to run an on demand backup of a .Net MVC app's SQL Express 2008 database to eg a flash stick plugged into the machine running the app.
I tried
QuickstemDataContext db = new QuickstemDataContext();
string quickstem_path = Path.Combine(save_path, "quickstem.backup");
db.ExecuteCommand(string.Format("BACKUP DATABASE {1} TO DISK = '{0}' WITH COMPRESSION;", quickstem_path, db.Mapping.DatabaseName));
But get the exception
Database 'quickstem' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally
I am using the following connection string.
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\quickstem.mdf;Integrated Security=True;User Instance=True"
Do I need to attach the DB using something like Express Management Studio and give it a name etc. Ideally I want to keep the app deploy very simple without having to setup sql management studio etc. Can this attaching be done another way or can a Backup be done with out needing to attach
I tried giving it the full path of the .mdf file instead of the database name but got a syntax error on c:
You'll find that if you add Database=Quickstem to your connection string, your backup code will work just fine.
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\quickstem.mdf;Integrated Security=True;User Instance=True;Database=Quickstem