3

I'm starting a new ASP.NET project, and I'm trying to follow the multi-project approach I've seen mentioned in multiple questions around Stackoverflow.

I managed to set up the connection string (I think) successfully, by placing it in my presentation layer's Web.config file.

<add name="MyDbContext" connectionString="Data Source=|DataDirectory|MyDb.sdf" providerName="System.Data.SqlServerCe.4.0"/>

However, when I run the following code from my BLL, no DB is created.

using (var db = new MyDbContext())
{
    db.MyEntities.Add(new MyEntity()
    {
        EntityName = "Entity 1"
    });
    db.SaveChanges();
}

So first of all, where will the actual sdf file be created? I'm assuming in my DAL project. And secondly, is there any further configuration I need to perform to get this to work properly? A link to a tutorial would be splendid.

I've tried following this tutorial, but my DAL project doesn't have a Global.asax file I can play around with.

Community
  • 1
  • 1
Cavyn VonDeylen
  • 4,189
  • 9
  • 37
  • 52

2 Answers2

2

where will the actual sdf file be created?

It'll be created in the presentation layer. Make sure you have an App_Data subfolder in there. That's what bit me the first time I tried it. Also make sure that you turn on Show all files in Solution explorer or open a Windows Explorer to the App_Data subfolder to see it appear there.

but my DAL project doesn't have a Global.asax file I can play around with.

The Global.asax is living in the presentation layer, not in the DAL. The problem with the tutorial you mentioned is that it makes use of only one project. In the real world you'll likely spread this out over multiple project, typical one ASP.NET (MVC) project and several Class Library template based projects. This causes things to be put in the right project.

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
  • Sure enough. I could have sworn that my App_Data folder in my presentation project was empty before. But viewing it in Windows Explorer showed otherwise, and now it's showing up in VS. Quick follow-up question, how do I refer to the seed methods if not through the Global.asax file? – Cavyn VonDeylen Aug 30 '12 at 14:52
  • Simply make a new thread for that question and point out in the comment. New questions deserve dedicated threads. – Kris van der Mast Aug 30 '12 at 14:54
  • New thread located [here](http://stackoverflow.com/questions/12200086/seeding-ef-database-in-a-separate-dal-project) – Cavyn VonDeylen Aug 30 '12 at 15:13
1
  1. |DataDirectory| is a macro that evaluates to ~/App_Data/ in your web project. So look for your .sdf file in path/to/your/project/App_Data/MyDb.sdf.
  2. You'll need to copy any other settings you added in App.config to Web.config.
jrummell
  • 42,637
  • 17
  • 112
  • 171