sergio
sergio

Reputation: 1046

Adding persistence awareness to POCO , EF5

[edited for clarity]

Here is my scenario Ive managed to create a POCO model of my db, consisting of only two tables, Blog and Post.[Model]

I want to create another project that references these classes and adds some db persistence code to it, which i dont know how. [DAL]

Later on, ill use this [DAL] inside the [BLL] to validate the data and present it to [Presentation].

In other projects i used EF but didnt create POCOS, so i ended up having data access code in the BLL and this time am trying to keep things strictly compartimentalized.

In the past, EF did everything for me, i just added a model and i could then call SaveChanges(). Now, do i have to write my self "insert..into..." in this new [DAL] im trying to create? there has to be an automated way

Upvotes: 0

Views: 312

Answers (1)

BrunoLM
BrunoLM

Reputation: 100331

I created a draft project of a Entity Framework using N-Layer model.

NLayerEF.Data contains the database model (.edmx file and .sql file)

NLayerEF.Domain contains ignorant persistent classes

NLayerEF.Infrastructure contains a class that inherits from DbContext, which is what you need to communicate with the database. Adding a property

public DbSet<TYPE> EntitySetName { get; set; }

you will be mapping a table described on your .edmx file to this property

public partial class StackOverflowEntities : DbContext
{
    public DbSet<Post> Post { get; set; } // Table Post
    public DbSet<User> User { get; set; } // Table User
    public DbSet<Tag> Tag { get; set; }   // Table Tag
}

Upvotes: 1

Related Questions