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?