Syma
Syma

Reputation: 584

Struggling With Entity Framework 4

Please I need help on how to handle CRUD in ef4 way. I started a project on asp.net mvc1 and ef v1 .net 3.5 sp1, but along the line after the release of vs 2010 I converted the application to asp.net mvc2 and ef4 .net4 after some reading on the new features both technologies offers.

Right now am kinda stock as things don't appear as easy as I thought, and the deadline for the project is fast approaching. I'll appreciate some help mainly on how to create, update and delete without doing database trip where it's not needed.

I found a way to handle the CRUD commands without the database trip, but the issue with this approach is that it overwrites all unchanged and database generated values (getutcdate(). etc)in the database and it does not work for the object graph (the object relationship). Please any quick advice on how to do things right? The code for my update command is this.

dc.PersonEntitySet.Attach(entity);ObjectStateEntry entry = dc.ObjectStateManager.GetObjectStateEntry(entity);entry.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);

Am currently using POCO on the client and manually convert it on the DAL to entity framework entity before doing the ef operation.

Thanks.

Upvotes: 0

Views: 362

Answers (2)

user1141441
user1141441

Reputation: 41

  • create a DataModel assembly using ADO.Net data model ( uses normalized database);

  • Remove all navigation properties (If your database is thoroughly normalized with lots of foreign key relations, you better do this)

  • Change default code generation setting to None and Change Metadata artifact Property to "Copy to output Directory" (This will generate the schema files in the bin folder that you can copy it to your DAL or WCF And it helps in code isolation)

  • Use ADO.Net POCO generator to generate POCO classes

  • Save the edmx file and build the assembly. (Saving the designer page and closing refreshes code)

  • Copy the code files to a separate assembly Data Transfer Objects This will be your types for refering to your entities; You can use them in BLL, DAL and WCF

  • In the DTO assembly removed the key words "virtua". This will prevent EF over-riding the generated code.

  • Create DAL (Data Access Layer) as separate assembly. (It is very important to add a reference to System.Data.Entity otherwise the context will be inacessible) It is good to keep the DAL in isolation.

  • Copy the context files into the DAL ; Add a reference to DTO assembly; Copy the 3 schema files from DM bin folder to the bin folder of this assembly.

    You will benefit with code isolation; when you need to add more entities or stored procs, simply copy the code files and replace the 3 schema files

  • Create another assembly called BLL (Business Logic Layer) and add a reference to the DTO and DAL assemblies; Copy the 3 schema files from the data model bin folder to this bin folder; Build the BLL assembly;

    It is always good to keep business logic in isolation.

  • Create WCF Service that refers to the BLL and DTO Since the types in DTOs are primitive types, all the serialization is taken care by WCF.

  • If you using win forms client add a service reference; This brings the entities that you can use it at compile time.

Hope this helps.

Upvotes: 0

Rick Rat
Rick Rat

Reputation: 1732

Try using the EF4 poco templates. But for better change tracking, I would use the self-tracking entities.

Upvotes: 1

Related Questions