Daily Archives: July 22, 2012

When Entity Framework 5 (EF5) is not Entity Framework 5 (EF5)

Installing EntityFramework 5 to my project.

image

EF5 has been successfully installed.

But I installed it to a project targeting .NET 4, not .NET 4.5. Check out the version of Entity Framework that got installed. It’s not 5.0.0.0. It’s 4.4.0.0.

SNAGHTML16ea8760

So what’s with that version number? Here is the folder created when I installed the EF5 package. (EntityFramework.5.0.0.-rc).

There are two folders in the lib folder – net40 and net45. Inside net40 there’s an EntityFramework.dll that has the product version name “5.0.0-4c.net40” and its File version is 4.4.20502.0.

SNAGHTML16f06907

In the net45 folder is the EntityFramework.dll file that is version 5.0.0.0.

The 4.4 version understands what’s in System.Data.Entity.dll in .NET 4.

The 5.0 version understands what’s in System.Data.Entity.dll in .NET 4.5. .NET 4.5 is where enum support lives, where System.ComponentModel.DataAnnotations.Schema lives, where System.Data.Spatial lives, etc.

We have two versions of EntityFramework coming via a single NuGet so that YOU don’t have to worry about downloading the correct version to align with the version of .NET that your project is targeting.

This makes a lot of sense, but there are a lot of people who ask me “I just downloaded EF5 but where are the enums”.

Hope this clears things up for some of you. Smile

Email Q&A: Entity Framework Modified State and SaveChanges

I get a lot of Entity Framework questions in email. Some from friends, some from people I don’t know. I like to help & answer when I can but this format means that nobody else gets to learn from the question. That’s one of the benefits of asking (or first researching) on the forums, stackoverflow.com and other community forums are so great…others can learn from your questions. Or you can learn from theirs!

Here is one of the 10 or so questions I got over the weekend. I was happy to answer because it was an easy one for me. (They aren’t all so easy for me… Winking smile)

The developers was hitting a very common point of confusion about setting Entry.State with graphs.

Question:

Hi julia!

what am I missing…

I have code first entities:

MaritalStatus

{

int id {get;set}

string Description {get;set}

}

Person

{

int Id {get;set;}

string FirstName {get;set;}

string LastName {get;set;}

MaritalStatus MaritalStatus {get;set}

}

I am binding control DataContext to a selected person

I have text boxes bound to the name parts, I have a combo box bound to a list of marital status.

the bindings all work – I change the combo, I can see in the debugger that is putting the selected marital status on my bound object

I execute this code to save the person:

using (var context = new PersonnelContext())

{

context.Entry(Person).State = System.Data.EntityState.Modified;

context.SaveChanges();

}

the name parts persist, but the marital status doesn’t.

Any ideas????

 

Answer:

Setting the State of an Entry to modified affects ONLY the object whose state you are setting. I t will have no affect whatsoever on other objects in the graph.

By passing Person into context.Entry, the full graph attached to Person will also get attached to the context. But initially, everything will be marked as Unchanged. Then the code changes the state of the Person object to Modified. The rest of the objects in that graph remain Unchanged.

SaveChanges will only send an UPDATE command to the database for the Person object.

 

Hope it helps.

P.S. This same information is in Chapter 4 of Programming Entity Framework: DbContext, starting on page 88.