none
What exactly is new since EF4.0, and how do I get the latest? RRS feed

  • Question

  • Sorry if this is a dumb question, but I'm confused!

    Been using EF4.0 for some time. Last week I installed the ASP.NET MVC3 stuff, and discovered that this included EF4.1 (which I didn't even know was out 'cos I don't have time to read all these blogs, and Windows Update didn't tell me about it). One of the new features really interested me (being able to use a lambda instead of a string for the Include() method), which prompted me to try and use EF4.1 in another (non-MVC3) project. This failed, leading me to suppose that I was still using EF4.0.

    I just had a look at the EF team blog, to find out that EF4.2 is now out, although the main EF Developer Centre still says that EF4.1 is the latest. It also seems that the only way to get EF4.2 is via NuGet.

    So, can anyone please clarify the following for me...

    *) Is EF4.2 a full release, not beta or anything like that? If so, why hasn't the main EF page been updated?

    *) If I install EF4.2 via NuGet (which is inside Visual Studio), does it install it just for that project, or install it for any project, just like the normal installers do? If it installs it just for that project, is there a way to install it for every new project without having to remember to use Nuget each time I start a new project?

    *) Where can I find a list of what's new since EF4.0? I'm sure there must be one, but I can't see one anywhere.

    *) Will an existing project automatically use EF4.2, or do I have to do something to update it manually? How do I know which version I'm using?

    Sorry to ask all these questions, but I've spent quite some time trawling around the EF site and the blogs, and can't find any clear answers. MS used to supply a simple install package that updated your machine, and they used to have "What's new" pages that told you what was new. Don't know why they've made it so difficult this time round.

    Thanks for any help you can give.


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Sunday, December 18, 2011 4:59 PM

All replies

  • Sorry to follow up to my own post, but I have another question.

    I tried creating a new console app, and adding an EF model to it. As far as I can see, this must be using EF4.0, not EF4.1, as it gave a compiler error when I tried to use a lambda in the Include method.

    I downloaded and installed the EF4.1 package, just in case the one that MVC3 installed was only for MVC apps (can't see why it would be, but I wanted to rule out the possibility), but it didn't help. I do get a reference to EntityFramework.dll version 4.1.0.0., as well as System.Data.Entity.dll version 4.0.0.0, so it looks like I've got both versions referenced in there.

    Please can someone help me sort this out? I'm fed up of wasting time on something that ought to be so simple. Previous upgrades were straightforward. Looks like they've made a big mess of this one.


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Sunday, December 18, 2011 6:40 PM
  • you need to include the System.Data.Entity; namespace to have the Include Method with Lambda (don't remember where I read this)

     

     

    using System.Data.Entity;

     

     

    then you can:

                //use select to include navigation further down the path
                operatorBindingSource.DataSource = _context.Operators
                    .Include(o => o.BusinessUnitPermissions)
                    .Include(o => o.BusinessUnitPermissions.Select(bup => bup.FacilityAccesses));
    

     

     

    Monday, December 19, 2011 8:42 AM
  • Hi, thanks for the reply.

    I added a using for System.Data.Entity, which allowed me to use a lambda, but I could only refer to properties of the entity in question, not related entities. For example, I tried a test using the Northwind database, and the following code worked fine...

    IOrderedQueryable<Productproducts = db.Products.Include(P=>P.ProductName).OrderBy(P => P.ProductName);
    

    The ProductName property was the only thing Intellisense offered me when I typed the dot after P, and if I changed it manually to a related entity, such as Suppliers, I got a compiler error.

    Any idea what's going wrong here? For one thing, if Intelliense is going to offer properties of the entity, why did it only show ProductName and not any of the others? Furthermore, why didn't it let me use a related entity name? There's no point in using Include for a property on the same entity.

    Thanks again. Any further help would be appreciated. Also, any pointers to where I can find documentation on the new features would be great.


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Monday, December 19, 2011 2:04 PM
  • Anyone? Am I the only person who can't get this to work?

    Any comments welcome.


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Tuesday, December 20, 2011 10:35 AM
  • Hi Mr Yossu,

    Include with Lambda parameter is defined in System.Data.Entity.DbExtensions class, the Include method can only refer the entities' properties, so you should define the Navigation for your entities.

     public class Blog
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public ICollection<Post> Posts { get; set; }//Navigation
        }
        public class Post
        {
            public int ID { get; set; }
            public string Content { get; set; }
            public Blog Blog { get; set; }//Navigation
        }
    ---------------
    using (var db= new MyContext())
                {
                    var test1 = db.Blogs.Include(e=>e.Posts).FirstOrDefault();//load the related collections
                }
    

    EF 4.2 = Bug Fixes + Semantic Versioning-- http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx (add data migration)

    EF4.1-Update1: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=26825

    -------------------------------------------------------

    ADO.NET Entity Framework 4.1 introduces two new features:

    • The DbContext API is a simplified abstraction over ObjectContext and a number of other types that were included in previous releases of the ADO.NET Entity Framework. The DbContext API surface is optimized for common tasks and coding patterns.
    • Code First is a new development pattern for the ADO.NET Entity Framework and provides an alternative to the existing Database First and Model First patterns. Code First is focused around defining your model using C#/Visual Basic .NET classes, these classes can then be mapped to an existing database or be used to generate a database schema. Additional configuration can be supplied using Data Annotations or via a fluent API.


    Update 1 includes a small set of changes including:

    • Bug fix to remove the need to specify ‘Persist Security Info=True’ in the connection string when using SQL authentication. In the EF 4.1 release ‘Persist Security Info’ was required for Code First to be able to create a database for a connection using SQL Authentication. The update includes a fix to remove this requirement. Note that ‘Persist Security Info’ is still required if you construct a DbContext using a DbConnection instance that has already been opened and closed.
    • Introduction of new types to facilitate design-time tools for Code First. Update 1 introduces a set of types to make it easier for design time tools to interact with derived DbContexts:
      • DbContextInfo can be used to instantiate and interact with a derived context as well as determine information about the origin of the connection string etc..
      • IDbContextFactory is used to let DbContextInfo know how to construct derived DbContext types that do not expose a default constructor. If your context does not expose a default constructor then an implementation if IDbContextFactory should be included in the same assembly as your derived context type.

    -------------------------------------------------------

    If you want to upgrade to EF4.1, you should install the install in the previous link.

    How to upgade to EF4.2 from EF4.1----Just update your NuGet.

    I don't there are big difference between EF4.1 and EF4.2 except bug fixing and Data Migration.

    BTW, NuGet is powerful Package: http://nuget.org/

    Have a nice day.

     

     

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 21, 2011 7:25 AM
    Moderator
  • Hi Alan, thanks for the reply.

    I'm confused when you say "the Include method can only refer the entities' properties" as Intellisense is only showing me the one entity property, namely ProductName, and is ignoring all the other properties, such as the various IDs, the price, units in stock, etc. It's also ignoring the navigation properties, which of course are the ones I specifically want to Include.

    I'm not using code first, I'm working from an existing database, so I'm not the one that created the code for the entity classes. I created an ADO.NET Entity Data Model, and used the existing Northwind database. Once the edmx file was created, I right-clicked the designer surface and chose to add a code egenration item, and added an ADO.NET DbContext Generator. All this was following one of the EF team blog posts. The end result of this was that the entity classes were generated for me, and include the navigation properties. For example, the Product entity class that was generated looks like this...

    //------------------------------------------------------------------------------
    // <auto-generated>
    //    This code was generated from a template.
    //
    //    Manual changes to this file may cause unexpected behavior in your application.
    //    Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace EF4._1Test
    {
        using System;
        using System.Collections.Generic;
        
        public partial class Product
        {
            public Product()
            {
                this.Order_Details = new HashSet<Order_Detail>();
            }
        
            public int ProductID { get; set; }
            public string ProductName { get; set; }
            public Nullable<int> SupplierID { get; set; }
            public Nullable<int> CategoryID { get; set; }
            public string QuantityPerUnit { get; set; }
            public Nullable<decimal> UnitPrice { get; set; }
            public Nullable<short> UnitsInStock { get; set; }
            public Nullable<short> UnitsOnOrder { get; set; }
            public Nullable<short> ReorderLevel { get; set; }
            public bool Discontinued { get; set; }
        
            public virtual Category Category { get; set; }
            public virtual ICollection<Order_Detail> Order_Details { get; set; }
            public virtual Supplier Supplier { get; set; }
        }
    }
    
    


    As you can see, it did generate navigation properties.

    The code I tried looked like this...

    using System;
    using System.Linq;
    using System.Data.Entity;
    
    namespace EF4._1Test {
      internal class Program {
        private static void Main(string[] args) {
          NorthWindEntities db = new NorthWindEntities();
          IOrderedQueryable<Product> products = db.Products
            .Include(P => P.ProductName)
            .Take(20)
            .OrderBy(P => P.ProductName);
          Console.WriteLine("Found " + products.Count() + " product(s)");
          foreach (Product p in products) {
            Console.WriteLine("  " + p.ProductName + " is in category " + p.Category.CategoryName);
          }
        }
      }
    }
    
    This was just a simple console program, to test the features. As you can see, I can use ProductName in the Include lambda, but nothing else. However, if I run this, I get a run-time exception as ProductName is not a navigation property (obviously).

    If you comment out the Include line, then it runs fine, proving that the Product entity has a Category navigation property, even though Intellisense won't show it, and the compiler won't allow it.

    So I'm no wiser. As far as I can see, I have the extension Include, as it allows lambdas, but it's not allowing even all of the basic entity properties, and doesn't allow any navigation properties, even though they are clearly defined on the entity, as you can see from the foreach loop.

    Any idea what's going wrong? Let me stress again that I'm only going to be working from an existing database, I'm not using code first, so I won't be writing the entity classes myself. Not that it seems to make any difference.

    As for the info in the rest of your post, thanks for the info and links, but it doesn't really help. I already saw the posts you linked, but the info there is very vague. Great, so we have a DbContext API, and it's simplified, but in what way? What does it do to make my life easier? The only code I saw looked exactly like the code I've been writing for the last 18 months. I couldn't find anything that actually explained what has been improved or simplified.

    Out of the four bullet points you quoted, only the first looks like it has any direct interest, as I don't do code first (nor do a lot of other developers judging by the comments on most of those blog posts and the ones in the EF developer centre). The bug fix is only of relevance if you are already using 4.1, which I'm not (other than this small test project, which doesn't work).

    So, I still have no idea what benefits DbContext offers me. I also have no idea if there are any other improvements in the 4.1/4.2 releases. I only came across the lambdas in Include by accident in an MVC book I was reading, I couldn't find any info on the EF pages. It makes me wonder what other changes/improvements have been included that I don't know about.

    Finally, you didn't answer my question about NuGet. I know I have to use NuGet to get 4.2 (although I still have no idea why MS feel that a simple downloadable file is unacceptable), but I still haven't found out if this means I have to do this for each and every project, or if I can do it once, and have my machine updated so that all future EF stuff will use the latest version. My previous experience of NuGet implies that I have to repeat the process for each and every new project, which is one reason why I (and others) feel that NuGet is the wrong way to release such software. Why do I have to do this every time? In the past, I could download an update and forget about it. Why are MS making me do more work before I can start coding? Seems like a big step backwards to me. Maybe I'm wrong, and NuGet will update my machine, making it a one-time operation, but I haven't found any answer to this.

    Thanks again for the reply, but I don't feel I'm any further on. I still can't get Include to accept a lambda, and I still have no idea what (if any) benefits 4.1/4.2 would bring to me. I don't mean to sound ungrateful, I really do appreciate your reply, but I feel that MS have done a really lousy job of this. How do they expect developers to take advantage of new releases when they make it so hard?

    By the way, the EF developer centre web page still claims that 4.1 is the latest version. Not exactly encouraging people to upgrade are they?

     


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Wednesday, December 21, 2011 12:27 PM
  • Hi Mr Yossu,

    Thanks for your feedback.

    >> Once the edmx file was created, I right-clicked the designer surface and chose to add a code egenration item, and added an ADO.NET DbContext Generator.

    The DbContext Generator is a part of EF4.1, in other words, you are using Code First in your project now, you can refer this link for detail information: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx

    Based on my testing for Include method, it works on my computer if I import System.Data.Entity in my class:

     var products = db.Products
            .Include(P => P.Order_Details)
            .Take(20)
            .OrderBy(P => P.ProductName);
    

    Would you please share your screenshot for the intellisense of Includen(p=>p.Order_Details), I'd like to help you.

    I hope this link helps: http://blogs.msdn.com/b/efdesign/archive/2010/06/21/productivity-improvements-for-the-entity-framework.aspx

    BTW, Data Migration is a good feature for EF4.2: http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alan_chenModerator Tuesday, January 3, 2012 8:04 AM
    • Unmarked as answer by Mr Yossu Tuesday, January 3, 2012 5:07 PM
    Friday, December 23, 2011 9:14 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 29, 2011 6:53 AM
    Moderator
  • Hello Alan, sorry I didn't reply before, but I've been away for eleven days, so only just seen your reply.

    The DbContext Generator is a part of EF4.1, in other words, you are using Code First in your project now, you can refer this link for detail information: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx

    Thanks, I've seen that one already. I'm still not clear what benefits dbcontext gives me over the object context way we were using before. I keep seeing posts telling me it's better, simpler, etc, but it looks extremely similar to me, and I can't see what's improved.

    I mentioned this before, but still don't have an answer. What specific benefits does dbcontext give me over the way I've been doing it for the last 18 months?

    Based on my testing for Include method, it works on my computer if I import System.Data.Entity in my class:

     var products = db.Products
            .Include(P => P.Order_Details)
            .Take(20)
            .OrderBy(P => P.ProductName);
    

    Would you please share your screenshot for the intellisense of Includen(p=>p.Order_Details), I'd like to help you.

    Please take a look at this...

    This is the project I described in my previous post. All it includes is an EF model based on the Northwind database. As you can see, Intellisense only offers me the product name, which is a string property (and so not really a logical option for Include), ignores all other scalar properties, and doesn't show any navigation properties at all.

    As I mentioned in an earlier post, I can manually type in the correct navigation property, and it works, but then that loses the huge benefit of Intellisense.

    Also, I just noticed that one of the two main benefits I was expecting from using a lambda doesn't work. once I enter the name manually, I get the compile-time check for navigation properties, which is good, but if I rename the entity in the EF model designer, it doesn't refactor that change into my code. It doesn't seem to matter if I rename the actual entity in the model designer, or right-click on the type name in some C# code and rename it from there. either way I get errors, as the renaming isn't performed correctly.

    This is really, really bad news.

    I hope this link helps: http://blogs.msdn.com/b/efdesign/archive/2010/06/21/productivity-improvements-for-the-entity-framework.aspx

    Thanks, I've printed that out for reading later, but glancing through it, it looks like the benefits are aimed at people doing code-first or model-first. As I already mentioned, I (and quite a lot of other people judging by the comments on many blog posts I've seen) never do this. I work from an existing database, so the benefits of code/model-first are of no interest to me.

    BTW, Data Migration is a good feature for EF4.2: http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx

    Again, this looks like it's only of use for code/model-first. Correct me if I'm wrong, but apart from the use of lambdas in Include (if you can get it to work), what (if any) benefits has MS given for people working from an existing database in EF 4.1 or 4.2? I'm not saying that code/model first isn't useful, I'm sure there are people very happy with it, but for what seems like the majority of real developers who work from an existing database, I have yet to see what 4.1 or 4.2 offer us?

    Thanks again for the post. I would be very grateful for any further comments you have. I realise I ended up asking quite a few questions in the the one reply, but it would be great if you can answer them all!


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/


    • Edited by Mr Yossu Tuesday, January 3, 2012 5:19 PM reattached image
    Tuesday, January 3, 2012 12:49 PM
  • Sorry, just noticed that the image I attached to the post didn't show up. I'll try again...


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Tuesday, January 3, 2012 5:09 PM
  • Hi

    I came across this post - and I've noticed that the original questions remain unanswered!

    I also can't find a proper set of release notes about EF 4.1 or 4.2. The ones published on the download page are a bit sparse - have any bugs/issues been fixed in 4.1 or 4.2?

    Also, please can someone help me out with new DbContext...

    I can see that it does not extend ObjectContext, but actually wraps it and provides the ObjectContext as a property.

    Also, DbContext doesn't support Self-tracking entities and you can't get at the underlying ObjectContext in a nice way - i.e.

    if you use ((IObjectContextAdapter)myDbContext).ObjectContext)  as  recommended, you have nothing to cast that to.

    So, for example if you had a NorthwindEntities ObjectContext, you could get at relevant properties, but if you cast a NorthWindEntites DbContext 

    as an ObjectContext you're left with the base ObjectContext class - which doesn't give you access to the ObjectSets, etc.

    Also, the comment about "To use the DbContext API in an N-tier application, consider using WCF Data Services or RIA Services. " Leaves some interesting questions about the way forward in terms of WCF services. You have "standard" WCF, WCF Data Service and RIA services - has anyone got a roadmap for where Microsoft is actually taking these 3 parallel technologies?

    Overall, I'm not left with a "good" feeling about the latest EF releases.

     

    Answers on a postcard :)  Any pointers/comments would be really appreciated.

    Tuesday, January 3, 2012 10:13 PM
  • Is anyone going to answer this? Alan Chen?

    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Thursday, January 5, 2012 5:39 PM
  • In case it helps anyone else, it turns out that the problem with Intellisense on the lambdas was a bug in ReSharper 6.1, and setting the Intellisense options back to the VS settings fixed the problem. Thanks to Fernando Soto for that.

    However, I (and Joseph by the looks of things) would still like to know if there is any decent documentation on EF4.1 and 4.2, as all I've found are a few scattered bits of info on blog posts, none of which go into any detail. How are we supposed to use the new features if MS won't document them?


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Monday, January 9, 2012 8:59 PM
  • Mr Yossu,,,, I have a question about Pixata gallery, could you pass me your e-mail address? thanks!
    Wednesday, January 11, 2012 1:40 AM
  • mryossu at hotmail dot com

    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

    Wednesday, January 11, 2012 11:34 AM