Why is my web.api project still looking for an .mdf file? - ef-code-first

I have a code first web.api project that was working fine with the automatically created .mdf in the App_Data folder. I decided to move my app to IIS and modify the app pool config to load the user profile, again, no problem. Then I loaded the .mdf in visual studios sql explorer and at that point, the app pool login from the web app started failing. No clue why and couldn't fix it, so I decided to use sql express instead of waste more time on it. So, I installed SQL Express, killed all my migrations, and modified the web.config connection string to:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=SSPI"/>
</connectionStrings>
When I try to use migrations to update-database or run the api app, I get an exception saying
Cannot attach the file
'C:\Users\Source\MyApp\Main\Solution\MyApp.API\App_Data\MyApp.DataService.DataModel.AppEntities.mdf'
as database 'MyApp.DataService.DataModel.AppEntities'.
Well, that makes sense since I deleted it, but why is my app still trying to connect to the .mdf file? It's gone and I've changed the default connection. I've searched the solution for anything referencing the .mdf file, but nothing shows up. What am I missing?

I finally figured out that I had to name my connection string the same as my DbContext class, so in my case:
<connectionStrings>
<add name="AppEntities" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=SSPI"/>
</connectionStrings>
to match my DbContext derived class
namespace MyApp.DataService.DataModel
{
internal class AppEntities : DbContext

Related

ASP.NET IDENTITY WEB FORMS

OK. I am experiencing something very strange with the asp.net identity in web forms. I setup the web.config file to establish connection to my local sqlexpress server. I previously created a database and ran the web application which uses asp.net identity and the 4.5 .net framework. The first time I ran the web app the tables were automatically created for me in the sql server database but then I noticed that in the APP_DATA folder under the solution there was another database created with a name aspnetdb.mdf
Although I have pointed out that the connection string connects to the sql server for some reason the web app connects to the newly created localdb database located in APP_DATA.
Am I doing something wrong here?
thanks
Oddly enough I have just come across this very issue in our codebase. One of my developers was using the 'old' Role provider to check if user was in a particular role. This was causing the app to create the database in App_Data as the config for all that still exists in machine.config. The connection string and provider details are specified in machine.config, so your web.config will only add or remove them. So for example if your machine.config says:
<connectionStrings>
<add name="LocalSqlServer" connectionString="..."/>
</connectionStrings>
And your web.config has this:
<connectionStrings>
<add name="MyConnectionString" connectionString="..."/>
</connectionStrings>
Your application will be able to see and use both strings. To fix it you can clear the connection strings first like this:
<connectionStrings>
<clear />
<add name="MyConnectionString" connectionString="..."/>
</connectionStrings>
But that still leaves the underlying problem that the old role (or profile) provider is being used in code. so you will instead get lots of errors when using it. You need to switch code like this:
if (User.IsInRole("Admin"))
{
//Do admin stuff
}
To something like this (in my case _userManager is of type UserManager<User> and is injected into my controller constructor at runtime.
var user = _userManager.FindById(User.Identity.GetUserId());
if (_userManager.IsInRole(user.Id, "Admin"))
{
//Do admin stuff
}

how to find the sql connection string in asp.net

I added a new Sql Server Database to my project with the name: ShDB.mdf. It is located in a App_Data folder.
It contains a Table which has some columns. I could add some numbers to the table and then show them in a gridview on my localhost.
This is what I have in the web.config:
`<configuration>
<connectionStrings>
<add name="ShConnectionStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Javad\Desktop\Sharj\App_Data\ShDB.mdf;Integrated Security=True;User Instance=True"/>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ShDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
</system.web>
`
Here is the vb.net code which I used:
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ShConnectionStr").ConnectionString
And it works properly on the localhost test but when I published the website and uploaded the files on a web hosting service to test, I counter an error. How may I fix it? I think I should change my Data source path but I don't know how because its my first experience. Thanks to any help. you can see the error page here: http://www.kaj.somee.com/SL.aspx
Sql Server is either not installed or not running on the web host, or you don't have access to connect to it.
ASP.Net does not by itself know how to use your *.mdf database. It needs a running instance of Sql Server to talk to. It will tell Sql Server to load (attach) your database file, and then send queries to the Sql Server service for execution.
Do you mean you already uploaded the website to a web hosting service, but not the database?
If so, your application may not communicate directly with your database which is actually in your local machine.
You probably need to upload the database to a database hosting service as well (eg.SQL Azure, etc).
You may need to figure out the IP address/Server Name of your database hosting where you have uploaded. These information you can probably get in for the service provider. From there you should be able to connect to the database with the correct configuration.

after changing asp.net mvc3 connectionstring, asp.net configuration dont work

i have a problem with the asp.net MVC3.
when i create a new project and start the asp.net configuration my browser opens and i can edit e.g. the user.
but when i change the connection string to my external server or to my local sqlexpress server, i get the message after the browser pops up:
The type "MyApp.MvcApplication" could not be loaded.
what do i wrong? my connection string looks like:
<add name="ApplicationServices"
connectionString="Data Source=my-pc\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
Looks like the type hasn't actually been built and packaged as a DLL into the bin folder. Did you try running the project initially before making the change?

"The connection string specifies a local Sql Server Express instance", Except it Doesn't

I'm trying to deploy an asp.net application to a server using a SQL Server instance for the ApplicationServices membership database.
The problem is, I'm getting an error that says
The connection string specifies a local Sql Server Express instance
using a database location within the applications App_Data directory
I got this error when I initially tried to deploy the aspnetdb.mdf itself with the application.
I got this error when I then scrapped that plan and decided to do a web.config transform so that in Debug I use the Express database, but on Release the connection string goes to SQL Server.
I got this error again when I decided out of curiosity to remove all references to the express database from the code, so there could be no possible way anything would be looking for the Express database. No luck.
Does anyone have any ideas about this? I have deleted and re-installed the web site in IIS each time, noting that there is no App_Data being deployed and no mention of the .mdf file in web.config - to no avail. It still thinks there's a connection string telling it to look for a SQL Server Express database :/
Edit: Here's the connection string I'm using. Pretty standard, I think, but I could always be wrong.
Data source=HERP;Initial Catalog=DERP;Integrated Security=True
Could the error refer to the default connection string from the machine.config file (the LocalSqlServer one)? This could be happening, considering that the default membership provider uses this connection string:
The following default membership element is configured in the
Machine.config file [...]:
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" [...]
connectionStringName="LocalSqlServer" [...]
Do not forget to clear the connectionStrings first:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

asp.net| change the place webforms save user data

I am trying to change the place my webform saves the data.
What I mean is, when you open a Visual Stuido/C# Web App, its has a user login/register data.
The default data is saved in your project folder, in APP_DATA as: ASPNETDB.MDF.
What about if I want to use MSSQL Server 2008R2.
How can I change it?
Just setup a new database and change your connectionstring in the web.config:
Using SQL Server instead of ASPNETDB.mdf
<connectionStrings>
<add
name="NorthwindConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
How to: Read Connection Strings from the Web.config File
in really server MS Server 2008R2 you must doing backup from your "home" datafiles: "in APP_DATA as :ASPNETDB.MDF"
p.s. sorry for my bad English.

Resources