I successfully followed the contoso university tutorial, getting it to work with SQL Express in VS 2010 - creating the DB in the App_Data folder. The tutorial follows the EF Code First model. Wanting to see the data in the tables, I opened the database in Server Explorer and was able to view the table data.
Now when I run it again, I can no longer access the database - the error given is:
One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.
Cannot open database "SchoolDB" requested by the login. The login failed.
Login failed for user 'DEV\bwilliams'.
Log file 'C:\ContosoUniversity\ContosoUniversity\ContosoUniversity\App_Data\SchoolDB.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously.
DEV\bwilliams would be windows authentication.
Here is my connection string:
<add name="SchoolContext"
connectionString="data source=.\SQLEXPRESS;Initial Catalog=SchoolDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|SchoolDB.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
I tried to open the DB again in server explorer and it tells me that the login failed. So I figured I could drop the database and have EF recreate it - but after doing so, I still receive the same error. The whole point is to create a project and deploy to an actual server, but I can't seem to get past these connection issues.
Update: Changing the name of the database in the connection string creates the new database and all is well. However, opening it up in Server Explorer in VS 2010 causes the same problem. Very frustrating.
Related
I've been trying to publish my application so I can install it on other computers but I always end up with an "SqlException" when trying it on the VS editor and when running it on the other computer
to make things clear I made the database using SqlServer and I attach it to the VS editor until now everything is going well and the application is running on the editor without any exceptions
but to make the application working on another computer I needed to copy the (mdf file from the SqlServer directory and paste it in the |datadirectory| of the project ) and of course I changed the connectionString dataSource to point to the mdf file and here where the exceptions begin to rise
The message of the exception says:
An attempt to attach an auto-named database for file
C:\Users\xXx\Desktop\Projects\Master Maint\Master
Maint\bin\Debug\GMAO.mdf failed. A database with the same name exists,
or specified file cannot be opened, or it is located on UNC share.
when I add the Initial Catalog=test in the connection string the exception message becomes:
"Cannot attach the file 'C:\Users\xXx\Desktop\Projects\Master
Maint\Master Maint\bin\Debug\GMAO.mdf' as database 'test'."
PS 1: It's not a permission issue cause I gave full access to the file from the advanced installer
PS 2: if there is the other way to run the database application on the other computer, I'm listening.
<connectionStrings>
<add name="connString2" connectionString="Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\GMAO.mdf ;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Thank you all , I'm not gone delete the question cause may others need it , but finally I found the answer
1 - the first bug was because the database file is not Categorized and that was solved by adding the Initial Catalog=test
2 - second the database file is named same us the the real database used in the project and that was solved by renaming the file
3 - third the log file that responsible of creating the database was not attached and that solved by adding the log file to the directory
Note: This is different from Cant find my EF code first database
He didn't specify the initialCatalog while I did as you will see:
I have 3 CodeFirst DBs that I work with.
1 I create and use in my visual studio project when I hit F5
1 I host in Azure with test users. If I want to update it, I simply remove it and recreate it by publishing my VS project
1 that is somewhere in my localDB, hosting it in my IIS in my machine, which is my issue. I can't find it! I don't know where CodeFirst places it. But I can access the website which somewhat brings records from it! Please help me find it.
Here is my entire setup: in my publishing profile for the Local:
lets call the blacked out "ProjAPIContext"
and in my IIS
and the www root folder is simple. The AppData folder is always empty:
and I have this in my web.config
<connectionStrings>
<add name="ProjAPIContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=ProjAPIContext; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|ProjAPIContext.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
the context matches what is in code:
internal sealed class Configuration : DbMigrationsConfiguration<ProjAPI.Models.ProjAPIContext>
That's my setup. I am looking for the DB so I can navigated through table rows and run sql queries against it, etc...
I have looked my entire harddrive with that ProjAPIContext. I see other DB created with the same times frame like tempdb and masterdb but they can't be those.
I have attach the debugger to connect to my server code in my visual studio, but nothing hit. I've run the profiler in my sql, it doesn't print the connection string. It prints useless junk.
UPDATE #1:
I also looked here:
Update#2:
of course I've looked there:
Where else should I look!?
First of all, I know this has been asked several times, but I have been searching the solution for a while and I couldn't find it, that's why I'm opening this.
I'm trying to open a service based database in Visual Studio 2010 which was sent to me in a C# project. I recently updated to SQL Server 2012.
In other threads the common solution was to delete the C:\Users\User\AppData\Local\Microsoft\Microsoft SQL Server Data folder, but it did not help for me.
I tried a few other things, but I might not have done it right, so I guess trying it again, if someone suggests that, would not be a bad idea.
Connection string I'm using:
<add name="Database1Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=DBName;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=False;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
UPDATE
With setting the user instance to false i can open the database now. However, when running the project i get the following error:
Thanks in advance.
When using the Add Controller helper/scaffolder with Entity Framework CRUD integration, I get the following error:
Unable to retrieve metadata for 'MyApp.Models.MyModel'. Cannot attach the file
'C:\Users\Lev\AppData\Local\Temp\2d7daf19-d47f-df24-ac76-d3df4fg2sa\aspnet-MyApp-
20130409170533.mdf' as database 'aspnet-MyApp-20130409170533.mdf'.
The question is: Why is it looking for the database in my temp files? The database file is located in:
C:\Projects\MyApp\MyApp\App_Data
This is my connection string:
Data Source=(LocalDb)\v11.0;
Initial Catalog=aspnet-MyApp-20130409170533;
Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnet-MyApp-20130409170533.mdf
And so it seems that the reference to |DataDirectory| is misbehaving. How can I set DataDirectory?
If I manually put the correct path into the AttachDBFilename property, then the scaffolding works. But I'd still love to leave it as |DataDirectory|.
Just restart the Visual Studio. This helped at my project.
Working with Entity Framework 4.1 with a SQL Server Express .mdf database .
For test purpose I am trying to perform CRUD operations on SQL Server Express database using Entity Model in a WPF application .
I am new to this concept, and I followed the video tutorial and done coding according that
I created Entity model of single very simple table. And I wrote simple code in cs file to perform adding one row to the database using following code
testEntities db = new testEntities();
TestTable tb = new TestTable();
tb.Name = txtName.Text;
tb.Email = txtMail.Text;
db.TestTables.AddObject(tb);
db.SaveChanges();
But if I go back check the database no data is added. Please tell me whats going wrong here ??
And here is my connection String
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="testEntities"
connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\test.mdf;integrated security=True;connect timeout=30;user instance=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
The whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf file (from the DataDirectory to the output directory of your running app) 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 on the .SaveChanges() call - and then inspect the .mdf file in the running app's directory 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. TestDatabase)
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=TestDatabase;Integrated Security=True
and everything else is exactly the same as before...