Naor
Naor

Reputation: 24093

Entity Framework 4

I decided to convert my DAL to Entity Framework 4.0.
I already have my huge database and also many business classes.
How can I map the tables and fields to my classes?

Upvotes: 2

Views: 247

Answers (2)

Andy White
Andy White

Reputation: 88455

Entity Framework allows you to create a mapping for your "conceptual" domain (your objects), and a separate mapping for the "storage" domain (your database). You then map these together using a central conceptual<->storage mapping specification. It sounds complicated, and unfortunately it is... EF is not a lightweight tool.

There's a lot to learn, but a place to start would be to read up on the CSDL, SSDL, and MSL specifications for Entity Framework.

If you want to use your current objects and database, you may have to define custom mappings in CSDL, SSDL, and MSL. If you want to make your life easier, you could generate a default Entity Data Model using the built-in Visual Studio tools. Creating an EDM from an existing database will generate objects that map to your database, but there are about a million ways to customize this process, and there are several ways to define your objects so that they can be used with EF.

I would suggest creating a default EDM from your database, and take a look at the .edmx file that is produced. An .edmx usually contains the CSDL, SSDL, and MSL in an XML format, along with generated code. (Again this can be totally customized). You might want to also spend some time reading up on EF... it's a beast.

Upvotes: 1

Kon
Kon

Reputation: 27441

There are many ways to skin this cat. AutoMapper is one that comes to mind.

You can also do it manually with LINQ, for example:

MyObject obj = (from e in entityModel
                select new MyObject {
                  ID = e.ID, 
                  Name = e.Name, 
                  // etc...
                }
               );

You can also write your own method that uses Reflection to map properties by type and name.

...Just throwing a bunch of ideas out there.

Upvotes: 1

Related Questions