0

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.

Community
  • 1
  • 1
voldemarz
  • 165
  • 5
  • 3
    I believe the template automatically hooks up a database file placed in the App_Data folder, and creates it if it does not exist. Try looking in App_Data. – driis Oct 18 '12 at 21:13
  • App_Data is empty. Application works until SQL Server is stopped. Works even if all connectionString are removed from config files (including default one in machine.config). – voldemarz Oct 19 '12 at 18:28

3 Answers3

0

I believe it is using SQL Server express, writing the DB files to the app_data directory within your application.

Charles Wesley
  • 828
  • 12
  • 27
  • I have SQL Server 2008 R2 Standard. But it wasn't used since I didn't set proper connection strings. And nothing was created in app_data directory either. As I understand it should have been created after I added this connection string: But it wasn't even when I added that connection string. The derived class was named MoviesDBContext. – voldemarz Oct 19 '12 at 15:55
  • Actually I am using SQL Express. Was intending to install Standard, bet settled with Express. So I guess it was using default connection string from machine.config as manman suggested. Any Idea then where that data was created if part of the string is like "AttachDBFilename=|DataDirectory|aspnetdb.mdf;" ? – voldemarz Oct 19 '12 at 16:23
0

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.

manman
  • 4,743
  • 3
  • 30
  • 42
  • I don't have SQL Express, I have Standard version. Entity Framework version 4.1.0.0, runtime version v4.0.30319. – voldemarz Oct 19 '12 at 16:07
  • Mistake - I do have SQL Express. Where is that DataDirectory that is referred in the connection string from machine.config "AttachDBFilename=|DataDirectory|aspnetdb.mdf;" ? – voldemarz Oct 19 '12 at 16:25
  • Have you checked your config file? If it's not there, then they have managed that in the Entity Framework code. Either case, you can override its default behavior by explicitly giving it your desire connection string (by adding it to your web.config file or in your code) – manman Oct 19 '12 at 21:57
  • I removed connections strings from both the web.config and machine.config. I queried the AppDomain.CurrentDomain.GetData("DataDirectory") and it actually points to App_Data of the web application. But it is empty, I even checked if the directory contains hidden files. I know I can override default behavior, I'm just trying to understand why is it working in this case. It shouldn't.. – voldemarz Oct 20 '12 at 16:38
  • I think it is hard-coded into the EF itself to use the SQL Expres if no connection string is defined. "it could provide no connection string at all (in which case our database would be created on the local instance of SQL Express, with a name similar to our executing application" http://www.alonintheworld.com/2011/11/using-adonet-entity-framework-41-with.html – manman Oct 20 '12 at 22:46
  • Thank you for helping to figure this out. Any idea why it wouldn't create the database file in the App_Data when I specify connection string with an name matching DBContext derived class? I have updated the question with excerpts from source and web.config. – voldemarz Oct 21 '12 at 12:51
  • You're welcome, I don't know whether it's mistyping here or it is really in your project also. Your class name is "MoviesDBContex" and your web.config file you named if "MoviesDBContext". You are missing the "t" at the of your class name – manman Oct 21 '12 at 20:50
  • Ok, pretty stupid... I indeed was missing a 't'. Now it works as intended with SQL Server Compact provider, but I get exceptions (added to the first post) when change that to SQL Server Client provider. Shouldn't that work also? – voldemarz Oct 21 '12 at 23:26
  • No, for using SQL Server you need to change your connection string as far as I know. check this site to find the formats suits your needs. http://connectionstrings.com/ – manman Oct 22 '12 at 00:15
  • Thanks, case closed. If anybody cares, here's an example of a connection string when using a SQL Server provider with arbitrary placed database file http://stackoverflow.com/questions/4116994/sql-express-connection-string-for-entity-framework-code-first – voldemarz Oct 22 '12 at 10:47
0

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

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Where is that DataDirectory that is referred in the connection string from machine.config "AttachDBFilename=|DataDirectory|aspnetdb.mdf;" ? – voldemarz Oct 19 '12 at 16:26
  • Out of curiosity I tried to remove that connection string from the machine.config, but the test web application still retrieved data. It failed only when I stopped SQL server. – voldemarz Oct 19 '12 at 16:28
  • @voldemarz - that is the App_Data directory under your project. – Erik Funkenbusch Oct 19 '12 at 16:28
  • Are you look at it in Visual Studio, or in Windows Explorer? Visual Studio may not update the tree to show it. – Erik Funkenbusch Oct 19 '12 at 19:18