I have 3 projects in my solution :
- An ASP MVC project
- A console app project
- A class library project (the DataAccessLayer)
I didn't want to recreate an ADO.net entity data model for each project so I've "simply" created a new class library project and added the ADO.net entity data model inside it. Is it an usual way to create a data access layer? Any improvements?
The DbContext (DAL project)
public partial class ModelContainer : DbContext
{
public ModelContainer(): base("name=ModelContainer")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Thread> Thread { get; set; }
public virtual DbSet<Post> Post { get; set; }
public virtual DbSet<Forum> Forum { get; set; }
public virtual DbSet<SubForum> SubForum { get; set; }
public virtual DbSet<Author> Author { get; set; }
}
Use of the DAL (ASP MVC Controller)
public class ForumController : Controller
{
private static ModelContainer db = new ModelContainer();
public ActionResult Index()
{
//returns a list of forums
return View(db.Forum);
}
}