Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Questions For Entity Framework Gurus

Questions For Entity Framework Gurus

Scheduled Pinned Locked Moved C#
csharpdatabasewcflinqhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm going through a decent EF 4.0 book, and a couple of things came to mind. I'm used to using LINQ To SQL, in my DAL I have standard CRUD methods, such as:

    public void UpdateCompany(CompanyEntity Company)
    {
    using (var dc = getDataContext())
    {
    var company = (from c in dc.Companies
    where c.CompanyId == Company.Id
    select c).First();
    if (company != null)
    {
    company.CompanyName = Company.CompanyName;
    company.IsActive = Company.IsActive;
    try
    {
    dc.SubmitChanges();
    }
    catch (Exception e)
    {
    throw e;
    }
    }
    }
    }

    1. If I understand what I'm reading correctly, you wouldn't really do this, as creating a new instance of the data context in each method won't work because of the change tracking in EF. It seems like you would use a single instance of the dataj context. I could use an example of a DAL that works with EF, if anyone has one. 2) Also, in my example I'm using my own entities, which are simple POCO's. Using EF I would use the entities that were generated. But they seem rather heavy compared to POCO's. In a situation where the DAL was accessed via a WCF app, would you use these entities? Thanks

    If it's not broken, fix it until it is

    E P 2 Replies Last reply
    0
    • K Kevin Marois

      I'm going through a decent EF 4.0 book, and a couple of things came to mind. I'm used to using LINQ To SQL, in my DAL I have standard CRUD methods, such as:

      public void UpdateCompany(CompanyEntity Company)
      {
      using (var dc = getDataContext())
      {
      var company = (from c in dc.Companies
      where c.CompanyId == Company.Id
      select c).First();
      if (company != null)
      {
      company.CompanyName = Company.CompanyName;
      company.IsActive = Company.IsActive;
      try
      {
      dc.SubmitChanges();
      }
      catch (Exception e)
      {
      throw e;
      }
      }
      }
      }

      1. If I understand what I'm reading correctly, you wouldn't really do this, as creating a new instance of the data context in each method won't work because of the change tracking in EF. It seems like you would use a single instance of the dataj context. I could use an example of a DAL that works with EF, if anyone has one. 2) Also, in my example I'm using my own entities, which are simple POCO's. Using EF I would use the entities that were generated. But they seem rather heavy compared to POCO's. In a situation where the DAL was accessed via a WCF app, would you use these entities? Thanks

      If it's not broken, fix it until it is

      E Offline
      E Offline
      Ed Hill _5_
      wrote on last edited by
      #2

      Am no expert here but my general approach is that you create a context in a using block per action you need to perform. For example Adding an Order item to a database create a single context, add the order and order items using that context then commit the changes. This means that if you action has one part that fails then none of it will get committed, and reduces the overhead of creating the context to one per action the user takes not creating one per item that needs adding to the database. I have also use EF in a service and it performs well enough for my needs, if it speeds up your development and performs well enough then why not use it.

      1 Reply Last reply
      0
      • K Kevin Marois

        I'm going through a decent EF 4.0 book, and a couple of things came to mind. I'm used to using LINQ To SQL, in my DAL I have standard CRUD methods, such as:

        public void UpdateCompany(CompanyEntity Company)
        {
        using (var dc = getDataContext())
        {
        var company = (from c in dc.Companies
        where c.CompanyId == Company.Id
        select c).First();
        if (company != null)
        {
        company.CompanyName = Company.CompanyName;
        company.IsActive = Company.IsActive;
        try
        {
        dc.SubmitChanges();
        }
        catch (Exception e)
        {
        throw e;
        }
        }
        }
        }

        1. If I understand what I'm reading correctly, you wouldn't really do this, as creating a new instance of the data context in each method won't work because of the change tracking in EF. It seems like you would use a single instance of the dataj context. I could use an example of a DAL that works with EF, if anyone has one. 2) Also, in my example I'm using my own entities, which are simple POCO's. Using EF I would use the entities that were generated. But they seem rather heavy compared to POCO's. In a situation where the DAL was accessed via a WCF app, would you use these entities? Thanks

        If it's not broken, fix it until it is

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        With EF 4, you can work with straight POCO's. Here's a sample of a simple POCO.

        public class Customer
        {
        public int CustomerID { get; set; }
        public string FullName { get; set; }
        public DateTime DateOfBirth { get; set; }
        }

        Now, to expose it for others to use

        public class MyContext : ObjectContext
        {
        public MyContext() : base("name="Myentities", "Myentities")
        {
        Customers = CreateObjectSet<Customer>();
        }
        public ObjectSet<Customer> Customers { get; private set; }
        }

        And that's it - a basic simple POCO with context and very, very lightweight. If you want to use the POCO over WCF, just change it so that it looks like this:

        [DataContract]
        public class Customer
        {
        [Key]
        [DataMember]
        public int CustomerID { get; set; }
        [DataMember]
        public string FullName { get; set; }
        [DataMember]
        public DateTime DateOfBirth { get; set; }
        }

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups