I'm using LocalDB and Entity Framework code-first for a new project. I want to change the location where the database is created/saved. However, every time I do, I get one of any number of exceptions. If I try adding AttachDBFilename=|DataDirectory|\database.mdf, I get an exception stating:
The underlying provider failed on Open.
As well as:
Cannot attach the file 'C:\Projects\database.mdf' as database 'Schema.MyProject'.
Eventually, when I run this program on another computer, I'm looking to create/save the database in the AppData folder. How can this be done?
Related
I have a c# .NET application with a SQLLocalDB database. I have used database first to create the EF6 model. I have added columns to one of the tables using SQL Server Management Studio and then used 'Update model from database' to propagate the changes into my model. When doing this, data on the development PC is unaffected and incorporates the new columns. However, when a different user runs the new version of the application, their existing datafile (.mdf) won't recognise the new columns and crashes with the exception:
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'VAT_long_description'
making the user's existing data in that table unreachable. I appreciate that Code First gives the ability to migrate data, but this facility appears not to be available when building the EF model using database first.
My connectionString is:
<add name="PMMEntities" connectionString="metadata=res://*/PMMData.csdl|res://*/PMMData.ssdl|res://*/PMMData.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\v11.0; AttachDBFilename=|DataDirectory|\PMM.mdf; initial catalog=PMM;Integrated security=True;MultipleActiveResultSets=True; App=EntityFramework""providerName="System.Data.EntityClient" />
I want to ensure that when a user installs a new version, their .mdf datafile will adapt to the new database schema without loss of data. It seems the only way to do this is through running a conversion or migration method on startup if the app throws the “Invalid column name” SQL exception. Where can I find the code (or a NuGet package) that will do this?
if you are using MVC , you have two methods to make an update in your models class database. the most easy way to resolve that issue is that you delete the model class related to the database and you import it again into your model folder. don't worry about data, they will still stored in your database. unless you have wrotten some extra object in the model class database, in this case you will waste those extra objects.. it will not affect your storage.
I am working on one project which is in Asp.Net web form and Sql server. When I run application I get the error Unable to connect Sql Server Session Database and inner exception is Cannot open database ASPState requested by the login.
I did some google search to fix this issue. I don't have sa password so i cannot logged in as sa to assign privileges to ASPState database for the newly created User.
I don't see any ASPState database on Sql Server when i logged in. So i thought I might need to run aspnet reg exe on my machine. So i tried to run aspnetreg_regsql.exe but i got the following error.
An error occurred during the execution of the SQL file
'InstallCommon.sql'. The SQL error number is 5170 and the SqlException
message is: Cannot create file 'C:\Program Files\Microsoft SQL
Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\aspnetdb.mdf' because it
already exists. Change the file path or the file name, and retry the
operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Creating the aspnetdb database...
I am totally blank now to solve this issue, I will really appreciate your suggestion for this. This issue hold to publish on production.
Dont think that there is a simple answer to this with the permissions that you have, I think that what has happened is that the database HAS been created, but the user you are using does not have any access to it at all.
Without having an administrators credentials I dont think that you will be able to resolve the problem.
You can however work around it by re-creating the database with a different name:
aspnet_regsql -d[YourNewDBName] -RestOfYourParamaters
That should allow you to re-run the creation script, however there is no guarantee that you wont end up with the same issue on the newly created database.
You should issue the command DROP DATABASE aspnetdb and then re-run your script.
'InstallCommon.sql' surely ain't any database. It must be a script that installs a new Database, as the name suggests. Run the script, and then link the generated database
I'm developing a cloud service using Azure Mobile Services, and in order to test it quickly and debug it, I want to deploy it in my local computer. I select the project, hit F5, got it running in IIS express in my local PC. I execute the cliente against my local URI address of the IIS service and when I try to insert a value, this exception appears if I try to retrieve or insert a new object:
An exception of type 'System.ArgumentException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The database name 'TR_MyTrip_Server_ExtraData_Service]_Persons_InsertUpdateDelete' is invalid. Database names must be of the form [<schema_name>.]<object_name>.
I debugged the Initialization of the controller and found out that mobile services deploys a LocalDb instance with this connection info on the DataBase property of the ServiceContext object:
Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MyTrip.Server.ExtraData.Service-20140731060708.mdf;Initial Catalog=aspnet-TestProject.Server.ExtraData.Service-20140731060708;Integrated Security=True;MultipleActiveResultSets=True
It is not able to find this DataBase if I try to connect with this connection string to the LocalDB via the SQL Management Studio
I'm able to access to the LocalDb instance with the SQL management studio running this command on CMD and retrieving the Connection for the LocalDB
SqlLocalDB.exe info v11.0
Eitherway, on the SQL Management Studio I'm not able to see any DataBase relating my controller of the Mobile Services. I searched on google and the only related link I found was this but is not working... Does anybody know what's happening?
Thank you so much
This is a problem when you have a project name with periods in it. Like TestProject.Server.ExtraData.Service.
To fix this, go into the web.config and edit the appsetting named "MS_MobileServiceName" taking out the periods. This is the value used as the SQL schema name for your mobile service tables in the database. It can't have periods or other special characters in it.
I generally try to make this the same as the name of the mobile service I'll deploy to, but it's not technically required.
For whatever reason, I couldn't get this working using the accepted answer. I assume this has something to do with the disclaimer in web.config which states:
After publishing to Mobile Services, these settings will be overridden by the values specified in the portal.
However, I did find another work around in the MobileServiceContext file where I replaced this line:
string schema = ServiceSettingsDictionary.GetSchemaName();
if (!string.IsNullOrEmpty(schema))
{
modelBuilder.HasDefaultSchema(schema);
}
with a simple string value modelBuilder.HasDefaultSchema('mySchema');
I imagine there's a way to influence the output of GetSchemaName() but, considering how much effort it took to resolve this problem, I am perfectly content with this solution for now.
If you've changed the MS_MobileServiceName in config to remove the periods, and you're still getting the error, you'll need to re-run the scaffolding for the Initial migration in the package manager console:
Add-Migration Initial -Force
From what I could see, the scaffolding generates an embedded .resx that still has your old schema name (DefaultSchema), and a snapshot (Target) that is referenced in the designer code. Just changing the DefaultSchema didn't solve the issue for me, but re-running the scaffolding did.
I also added Table annotations to my models, but I don't think that was the issue. And probably isn't ideal if your service is already live.
I am using VS2010 , and I built a .mdf file using SQL server 2008
I want to use this database file in my wpf application so that I can add rows to it and delete rows from it
the problem is , I can't access this file , and all the insertion and deleting is actually hapening to the datacontexct i created .
I used myDataContexct.Submitchanges() but it didn't work either
I tried to add a connection string when I define the datacontexct that holds the url of my .mdf file and this it gave me a runtime error when trying to access this file and the error messege says :
An attempt to attach an auto-named database for file Trial.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
please help me because I searched alot but I couldn't find any help
If the application is not going to be installed in a manner that many clients are accessing the same server, you would want to consider using SQL Server Compact Edition.
Are you sure the connection string in your app.config refers to the local mdf file? perhaps it refers to the server instance?
What technology do you use, is it LinqToSql or Entity Framework (I think you have t call SaveChanges, not AcceptChanges)?
If you do intend to access the server instance, then the problem seems to be a security restriction.
Please add more details on statement no. 1, and I'll write further info.
I am trying to move databases from one SQL server to another.I am doing this through a Console written in C#. The methodology is a follows. I first detach the database, move the data and log files to the new location and then attach the files from there.
However after I detach the file, I am not able to copy the data and log files. The error that I get is :'device is not ready'.
I am using network paths for both the source and destination. I'm using the File.Copy() to move the files and I have permissions to create files in the respective folders
At a hunch, 'device is not ready' is not an C# exception generated by the File Copy command. The possible exceptions are;
UnauthorizedAccessException, ArgumentException, ArgumentNullException,
PathTooLongException, DirectoryNotFoundException, FileNotFoundException,
IOException, NotSupportedException
So this 'error' could possibly be something which generated internally, as per the other comments, more information is needed. In order to debug this problem I suggest the following
Place a breakpoint in your code where 'device is not ready' text string is present in your code
Check both paths in your code are valid paths
Is your program is built to handle UNC paths or file paths?
Possibly an exception is being silently caught, but you'll need to debug to find this out
If your trying to move databases, have you stopped the Windows Service instance associated with the database? Type services.msc from the run command, then scroll to SQL Server (your SQL instance name) then stop the service from there. That would unlock the database file.
Good luck.