1

I am using entity framework, code first, 4.0, hitting an existing legacy database for read-only access. The database is normalized, so

 Table [Event]
      [ID]
      [Date_Entered]
      [Event_Status_Key]

 Table [Event_Status]
      [Event_Status_Key]
      [Event_Status_Description]

My class looks like

 public class Event
 {
       public DateTime DateEntered { get; set; }
       public string StatusDescription { get; set; }
 }

This is a WCF service layer application.

My two questions:

  1. Is there any easy way to populate the status description without creating a second Dictionary-type object? I've seen questions like this: Entity Framework Mapping to Lookup table, but they seem to be focused on object to object, and I really just want a primitive. I'd prefer using the fluent API as opposed to attributes.

  2. When the data is loaded, is any of the data cached at the code layer? Or does each check on the StatusDescription mean a separate call on the [Event_Status] table?

Edit: A possible (more subjective, which is why I didn't bring it up) third question is how close should the data entities match the database. Is it always a one-to-one field/table? Is what I'm doing (joining two tables into one data entity obejct) bad?

Thanks.

1 Answer 1

1

Entity framework expects that you will map both tables as separate entities and use projection in your query:

var query = from e in context.Events
            select new WcfEvent // Your original Event class is just DTO
                {
                     DateEntered = e.DateEntered,
                     StatusDescription = e.EventStatus.EventStatusDescription 
                };

This example expects correctly one-to-one mapping of your Event and Event_Status tables.

If you need any kind of caching you will have to implement it yourselves. Projected results are not even tracked by the context.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.