I am going through ASP.NET MVC 3 tutorial. Got to the point where EnityFramework was used for model classes. I ran the the application without having connection strings in the web.config specified and it worked.
I could add, edit, delete records. And the weirdest thing is that they are sill there even after I stop development server and start debugging application again as if table was created somewhere in memory and stayed alive for some reason. Can anybody explain what was going on?
Here's a link to an image:
oi48.tinypic.com/fnbeba.jpg
Was it supposed to use the the connection string for which name matches the name of the class derived from DbContext?
EDIT1:
Because I had no connection string in web.config it was generated by Enity Framework using namespace and the name of the class derived from DBContext. By default EF uses SQL Express, hence database file was created in database server's DATA direcotry C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA.
The last thing I don't understand why it wouldn't create the database file in App_Data dir if DBContext derived class is
public class MoviesDBContex : DbContext
{
public DbSet<Movie> Movies { get; set; }
// Passing name of the connections string shouldn't be even necessary
public MoviesDBContex()
: base("MoviesDBContex")
{ }
}
and web.config contains
<connectionStrings>
<add name="MoviesDBContext"
connectionString="Data Source=|DataDirectory|Movies.sdf"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Thist what the Microsoft guide about ER connections and models (msdn.microsoft.com/en-us/data/jj592674.aspx) says:
If the name of the connection string matches the name of your context (either with or without namespace qualification) then it will be found by DbContext when the parameterless constructor is used. If the connection string name is different from the name of your context then you can tell DbContext to use this connection in Code First mode by passing the connection string name to the DbContext constructor.
Any idea why database file isn't created in App_Data?
EDIT2:
There was a missing 't' in the class name and hence it didn't match the name of the connection string.
I does work as expected when provider is SQL Server Compact providerName="System.Data.SqlServerCe.4.0", but if I change it to providerName="System.Data.SqlClient" I get an exception added below. Shouldn't regular SQL Server be able to create a database file also?
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Do you have SQL Express installed on your machine? Which version of Entity Framework are you using? As long as I know the Entity Framework targets the local SQL Express as its default connection string if no string connection is provided.
I believe it is using SQL Server express, writing the DB files to the app_data directory within your application.
This is called "Convention over configuration", and Entity Framework and MVC use this principle a lot. Basically, it says that unless you give it specific instructions otherwise, they will assume various conventions.
If you don't supply a connection string to EF, then it will use a default connection string, generated from the namespeace and name of the class. If that doesn't exist in your web.config, then it will use the DefaultConnection that is defined in the machine.config located in the folder tree at C:\Windows\Microsoft.NET
Related
We store a template for building SQL Server CE connection strings in our web.config and use string.format to set the data source and temp path values.
In the web.config, the template for the DB connection string is:
"Data Source={0}; Temp Path={1}; Mode=Read Only"
In the web.release.config, we use to build deployment packages, the template for the DB connection string is:
Data Source={0}; Temp Path={1}
Now we only use the SQL Server CE database for read only operations. So, being a smartypants, I thought I would remove the connection string transform in the web.release.config and just always use the read only connection string from the web.config.
This worked fine for the database I was testing. But the same code running with a different SQL Server CE database fails with the following exception:
Exception 'System.Data.SqlServerCe.SqlCeException' with message 'The
database is opened with a read-only connection. Can't perform
post-initialization operations like re-building indexes and upgrading
public tracking. Please re-open with a read-write connection. [
Database name = D:\inetpub\wwwroot\MyApp\App_Data\Storage.sdf ]'
We are using the System.Data.SqlServerCe.4.0 provider
Why in my release build that is deployed to a server does read only access fail for some SQL Server CE databases but not others? It is the same code, just pointing at a different .SDF file. The .SDF files have the same schema, just different data.
Is the driver trying to rebuild indexes because the .SDF file is in some older format? Should we never use a readonly connection in production? Is that why the original code had the transform? why do they all work with a readonly connection on my Windows 7 workstation?
You should always specify a temp path, as moving databases between OS platforms/versions can require index rebuilds.
See my blog post post here: http://erikej.blogspot.dk/2009/08/running-sql-compact-from-cd-rom-read.html for more detailed info.
Well, I have two projects:
Blog.Core (a class library project for my entites as well as data access code)
Blog (ASP.NET MVC Project)
I installed EF in Blog.Core. Then added reference to Blog.Core in Blog. I have specified my connection string in Blog.Core since this is my DAL. Now when I run the project I get the following error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I deleted the connection string form Blog.Core and placed it in Blog project, and it works. Why is that? Doesn't this break the architecture because client-side has the database connection string? I mean isn't that correct that only DAL should be responsible for connection strings and database operations. Why was my first (and, according to me, more logical) solution not working?
There has been lots of argument about this one and you can refer to this thread C# DLL config file
I am creating a web application in ASP.NET MVC5. when i created the project a controller named AccountController was added automatically with all the necessary functions. now when i run the application and register a new user or try to login it gives some error. When i run it, it should create a database in App_data but its not creating any database and giving the following error. can anyone help me solve it please
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)]
the connection string is:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-EmployeeOrderSystem-20160710042802.mdf;Initial Catalog=aspnet-EmployeeOrderSystem-20160710042802;Integrated Security=True" providerName="System.Data.SqlClient" />
if any other detail is needed please ask. Thanks in advance
When you create a new MVC 5 application and choose "Individual User Accounts", a new ASP.NET Identity Provider is included which uses Entity Framework 6 Code-First.When Identity is accessed for the first time, Entity Framework checks to see if the database exists. Unless configured otherwise, it uses the "DefaultConnection" to find the identity database. If the database does not exist when Identity is called, EF automatically created the database.When the first user is registered, database to be created.
I have written a trivial Entities code-first WinForms application with one simple class and one database context class as all the tuturials describe it.
But I did not add a connection string in the app.config file.
Nevertheless, when I start the application, it can insert objects into the database and even show all objects already inserted.
I figure there must be some default database in SQL Server for that case but I cannot find out which instance and database name is used.
Database name is the same as the name on your DbContext class. By default it creates a database on the local machine Sql Server express installation. You should be able to see it in Sql Server Managment when you connect to your local SQLEXPRESS.
If you spesify a connectionstring with the same name as youd DbContext it will use that instead.
I'm testing how code first works. Here's how I defined the Context
public class EfDbContext: Context
{
public DbSet<Client> Clients { get; set; }
}
I didn't create any data base, but I was able to do all the CRUD operations.
Now I don't see the connection string in my web.config. I don't see either the Database. I've checked the App_Data Directory and also the Sql Server Express. I don't see any trace of the Database created.
Yet, everything is perfectly.
EF code-first will use a connection string that has the same name as your DB context - so you could define it like this:
<connectionString>
<add name="EfDbContext"
connectionString="server=YourServer;database=YourChoice;Integrated Security=SSPI;" />
</connectionString>
The database won't be created by default, until you actually do something, e.g. it should show up as soon as you make a call to EfDbContext.SaveChanges() for the first time.
It will be called the same as your DB context (YourNamespace.EfDbContext) if you haven't defined your own, custom connection string, and it should show up in your default local SQL instance.
See from the ADO.NET EF 4.1 Code First Walkthrough:
Where’s My Data?
DbContext by convention created a database for you on
localhost\SQLEXPRESS. The database is named after the fully qualified
name of your derived context, in our case that is
“CodeFirstSample.ProductContext”. We’ll look at ways to change this
later in the walkthrough.