0
votes

I'm using EF5 Model First. I don't really understand what are the auto-generated classes from the EDM. According to some documentation this classes are POCOs but why are they used in the context ? Assuming I have a Student entity, then I get a Student POCO class and a DbSet StudentSet property in my context. Will this next instructions put a POCO in my database ?

MyContext.StudentSet.Add(johndoe);
MyContext.SaveChanges();

So EF uses POCO to transfer data ? Actually I miss the step when POCO exchange data with entities or DTO and when the entities put data in the database.

1
What have you tried, yes, your code will put JohnDoe in your database. If you carry data on DTO, then you need set POCO based on the properties from DTO.J.W.

1 Answers

2
votes

The generated classes from the EDM is the ORM / Persistence classes. You use that classes to query / make changes from / to database. You need to translate any DTO object to POCO object when about making changes to database.

ORM is about mapping object to data in database, instead of dealing with insert into syntax to insert record to database in the application, you use StudentSet.Add to add a new data. The johndoe information will be translated into sql syntax, EF will map each property to each column when translating it into query.

The Add method will store the johndoe information as Added in the memory but it will not be executed right away to the database. If you have another Add method, it will be marked as Added too. The moment you call SaveChanges, all the changes will be saved into database by sending a generated query.

The mapping between DTO and EF entity happens before you add the johndoe. You might have another DTO class that is used in the UI. You need to map it manually or using mapper library to create a POCO object from a DTO object. For example:

// studentDto as parameter

var johndoe = new Student
{
    Name = studentDto.StudentName,
    Age = studentDto.StudentAge
};
MyContext.StudentSet.Add(johndoe);

// studentDto might have another information as well
var johndoeSubject = new Subject
{
    Name = studentDto.SubjectName,
    Years = studentDto.SubjectYears
};
MyContext.SubjectSet.Add(johndoeSubject);

MyContext.SaveChanges();