Where is the DB that CodeFirst created? - c#

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!?

Related

How to make winforms database application that run on clients computers

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

Not publishing a SQL connection string on deployment when it already exists using Transforms

TFS 2015, vNext build process (not xaml)
I'm trying to automate the deployment process for an MVC web app. The servers that it would be deploying to have different SQL connection strings. So my current deployment is to copy the web.config file to web.config.save manually, deploy the new version using WebDeploy then go to the old web.config.save file and copy over the connection string. I'm to the size now where this is not going to be sustainable much longer. So I need to automate this process further. I've got TFS automatically building and deploying to my test server (which also has a different connection string so it's a great test). In the "publish" properties, I tried deselecting the "Use This connection string at runtime (update destination web.config)" checkbox in the publish-settings-database section then checked in the project again to TFS. But when deployed, the web.config took the default setting. I don't want to remove this setting because I want a default connection string there for new installs.
I could write an xml transfer program to save off the current connection string then overwrite when complete again. But I figured there must be a way to do this with the current tool set and why try reinventing the wheel?
I started down the path to use transforms. So I created a web.config.release in VS2015 then added in this into the web.config.release file:
<connectionStrings>
<add xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" name="AppEntities" connectionString="metadata=res://*/Models.DBName.csdl|res://*/Models.DBName.ssdl|res://*/Models.DBName.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLExpress;initial catalog=DBName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
So, does anyone know how I can make the SQL connection string update only if it doesn't exist in the current web.config for deployment?
I got this experience with TFS 2015 vNext build,
in the 2Q of 2016 we assigned a Dev Ops person, who was really good with PowerShell, and he took ownership of the build and deploy process. Some of the things he came up with was a post build script.
Now to answer your question: For us, the Post-Build script would do the deployment part, and as part of the deployment you would update the connection string in the web.config. Now this would require someone in your team to become really good with PowerShell.
Now what I would do is write a special word in the original file like "{ConStringHere}", and the replace it with the Powershell script, that way you'll only replace it once.
See this how to verify if a file contains a word and replace it using PowerShell:
Powershell, using contains to check if files contain a certain word
I'll highlight some of the benefits we have encountered with this approach:
You don't need a solution, project and classes to modify some files. Most post-build files modifications can be done in powershell with line or 2.
You can keep the powershell scripts in TFS keeping a history and comments of the changes.
You can have the special values (Connection Strings, users, passwords) defined as build variables that are easier to change and with higher security than hardcoded values in a project.
You can have multiple scripts per environment. Ideally all scripts should be almost the same per environment, but maybe in production you are publishing to 2 different servers, so you'll need to call a line twice.
You'll save developers time, since the person maintaining the powershell doesn't have to be a full stack developer.

Database connection issue in MVC3 project

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.

Why could "update database" be missing in visual studio 2012 when publishing?

I'm trying to deploy an application to my webserver and everything works, except that it does not update the remote database.
With a new demo MVC project you have the option "Update database" when publishing
For some reason that options is not available when I want to publish my own MVC project.
Why is this option not showing?
I am using EF (database first)
I am getting a warning under the connectionstring fields: in order to publish a code first model you must enable EF code first migrations. (although this project should not use code first)
Web Deploy 3.0 and the dbDacFx provider are installed on the server
-- EDIT --
I have now recreated the project, and the update database keeps showing until the first web publish. then it just disappears and shows the warning in order to publish a code first model you must enable EF code first migrations. (although im not using code first, im using database first) Is this a bug in VS or what am I doing wrong?
--FIX--
I got it working by adding another connection string pointing to my database (without EF interference) in web.config: <add name="DeployCode" connectionString="Data Source=(localdb)\Projects;Initial Catalog=xxxxx;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;" providerName="System.Data.SqlClient" />

Entity Framework 4.1 is not adding any rows to SQL Server Express database

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...

Categories

Resources