1

I'm following these two tutorials to understand how EF works.
http://msdn.microsoft.com/en-us/data/jj193542
http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

There is a difference between these two tutorials : The first one says that I don't have to create a connectionString under Web.config for EF to create a new DB, but Scott Gu's tutorial says I need to.

I succeeded with the first one:

namespace LearnDB.Models
{
    public class Person
    {
        [Key]
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

namespace LearnDB.DAL
{
    public class DBAccess : DbContext
    {
        public DbSet<Person> Persons { get; set; }
    }
}

namespace LearnDB.Controllers
{
    public class HomeController : Controller
    {
        public string Index()
        {
            var db = new DBAccess();
            var p = new Person { Name = "A", Age = 1 };
            db.Persons.Add(p);
            db.SaveChanges();
            foreach (var per in db.Persons)
            {
                return per.Name;
            }
            return "hi";
        }
    }
}

This program runs successfully.
However, I'm wondering:
1. Why my program succeed without me adding any connectionStrings? I thought I have to add
<add name="DBAccess" ....../>. If it's not necessary, why Scott Gu says I should add it?
2. What exactly triggers the automatic creation of the Database? From what I have tried, the DB won't be created on compiling or creating DbContext, but only on some data being added to it. Is this correct?

1 Answer 1

2

Entity Framework has conventions that will do many things by default if you don't override or otherwise specify it. This includes everything from whether a database will be created for you, where it will try to do this, and how it will interpret your classes that you've put into the DBContext.

Since you did nothing, the answer was given in the first link you provided: http://msdn.microsoft.com/en-us/data/jj193542

Go down to: "Where's My Data?"

Since you didn't do anything to override or define the settings you wanted, EF will try to create a database for you (if one doesn't exist). It says if you have SQLExpress installed it will try to use that, but if not it will try to use LocalDB (which is a newer even lighter weight version that is pretty good for developers to minimize their development footprint).

Scott Guthrie's article is pointing out yet another convention. It will use a specific connection string that is named the same as your DBContext class as the connection string for your database.

However, if you don't supply that (which you didn't) go back to the first article, where they say they will name the new database they create (on either SQLExpress or LocalDB): "The database is named after the fully qualified name of the derived context".

Basically, what you've done is the least amount needed just to get something going. That's not a bad thing. It just means it is likely mysterious what all the conventions are. Convention over configuration is a nice concept, but definitely daunting when you don't even know what the settings are that are being set for you!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.