July 1024

Entity Framework 4 - my first steps

I’m working on a pet project that I’ve wanted to migrate to using an ORM for a while. Tonight I took the plunge and decided I’d see how easy Entity Framework 4 (EF) makes things.

The Code-First Development with Entity Framework 4 post from Scott Gu made it look pretty easy and I figured I could probably swap out all my existing data access layer quite quickly, after all it’s promising to do most of the dirty data plumbing for me. Sure enough it’s really quick and easy to create a bunch of POCO classes but I’ve already hit 2 gotchas that I thought I’d share:

  1. Nullable datatypes for primary keys are not allowed. Typically I’ll create public int? ID{get; set;}, if the ID isn’t known then this is a new entity, if it is known then I've retrieved the object from the database and so this will be an update. However nullable datatypes are not accepted, so I’ll have to modify the way I’ve been doing these things.
  2. Object inheritance. I have a Contact class that defines some basic properties contacts have, then 2 derived classes (Customers and Suppliers). As all contacts should have an address I want to add an Address property to Contact but I’m getting an error.

    The navigation property 'Address.Contact' references an entity type 'Data.Models.Contact' that is not contained within an entity set. Reference a specific object set type 'Data.Models.Supplier,Data.Models.Customer' instead.

    So instead of having 1 place to deal with the Contacts Addresses relationship (and in this case it is only very simple, contacts are allowed 1 address only), I now need 2 places?

The code

This is what I've got for the Contact class

using System;

namespace Data.Models
{
    public abstract class Contact : BaseEntity
    {
        public virtual Address Address { get; set; }
        public DateTime DateAdded { get; set; }
        public String Email { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Title { get; set; }
    }
}

and this is for the Address class:

using System;

namespace Data.Models
{
    public class Address : BaseEntity
    {
        public virtual Contact Contact { get; set; }
        public String County { get; set; }
        public DateTime DateAdded { get; set; }
        public String Phone { get; set; }
        public String Postcode { get; set; }
        public String Street { get; set; }
        public String Town { get; set; }
    }
}

finally this is the Supplier class:

using System;
using System.Collections.Generic;

namespace Data.Models
{
    public class Supplier : Contact
    {
        public virtual ICollection<Component> ComponentsSupplied { get; set; }
        public virtual ICollection<Product> ProductsSupplied { get; set; }
        public String SupplierName { get; set; }
    }
}

So nothing overly complex going on here – Suppliers implement Contact and provide a SupplierName property. I’m not sure I get why EF won’t let me define my classes this way.

That said this will make me significantly more productive once I get over this initial learning curve. I’ve been wanting to implement NHibernate in this project for a little while but messing about with xml files compared to the quick approach with EF means that I’ll probably use EF for all simple things for the moment – well until I get something I can experiment with using NH Open-mouthed smile

Permalink | Comments (1)

Comments

  1. trackback WOMBAT (Saturday, July 31, 2010) #

    SaveOrUpdate with Entity Framework, mostly update though!

    SaveOrUpdate with Entity Framework, mostly update though!

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading