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();