Using EF with an existing database and a domain / object model that is not 1: 1

First of all: I'm new to EF, and it's just checking the capabilities of EF and seeing how it fits into an environment that is already defined in terms of database tables and an object model - and when these two models do not display 1: 1 in terms of class names / table names and attribute names / column names and even the number of tables and classes. Please keep the “feature check” in mind if you feel like responding: “EF is not intended to be used that way.” Thank you :)

Consider the following scenario.

I have three database tables:

|----------------| |-------------| |-----------------| | MessageLog | >---1 | Message | 1---< | MessageText | |----------------| |-------------| |-----------------| | logId | | messageId | | messageId | | messageId | | type | | languageCode | | from | | ... | | text1 | | to | |-------------| | text2 | | ... | | ... | |----------------| |-----------------| 

What I want to map to two classes:

 |--------------------| |----------------------------| | ServiceMessage |<-\ | ServiceMessageInstance | |--------------------| | |----------------------------| | Id: string | | | Id: string | | LanguageCode: str. | \----| Message: ServiceMessage | | Text1: string | | From: DateTime | | Text2: string | | To: DateTime | | Type: MessageType | |----------------------------| |--------------------| 

ServiceMessage objects consist of a row from the message table (with the key messageId ) and a row from the MessageText table (with messageId and languageCode as).

ServiceMessageInstance is simpler: it refers to a ServiceMessage object and some additional attributes.

I played a little with EF and did a lot of search queries, but I did not find the answer to my question: how can I do this with EF? Which approach should I take? Can I do this with an EDMX designer? DbContext.OnModelCreating?

I understand that this may be too broad a question, but I would appreciate any pointers!

Thanks.

BR, Herms

+4
source share
2 answers

In what form you can ask a question :-) It is well explained here: http://msdn.microsoft.com/en-us/data/ee712907

To help you, you can also install the power tools of the Entity Framework.

http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

There is a good option to reverse-engineer code first from the database.
Create a new project. Then in the Right Click project, the context menu should offer enter image description here

You should see the code added to your project. This may be useful for review. Then delete the temporary project. after using useful parts for your own code.

Luck

0
source

Honestly, I didn’t actually try to do this because I didn’t come across a situation that made me do this, however, I remember that when I read “ Entity Framework DbContext Programming ” I came across the section in chapter 5, to accurately name “Matching a single object across multiple tables,” which, I think, does exactly what you want.
NOTES

  • The aforementioned function, called "Separation of Entities" , first uses the root code of the entity, but I assume (not sure) that this is possible with other myths of the structure of the entity.
  • You cannot enable it using Data Annotations; instead, you will have to configure it using the Fluent API.
  • To do this, you need to use the Map function.
  • There is also a function that goes in the other direction, and allows you to display multiple objects in one table.
0
source

All Articles