Entity Framework Code First

I’m trying to wrap my head around the latest update to Entity Framework 4.1. I’m trying to use Code First to create an object model that will map successfully to an existing database.

So far I think it will work for my needs and I’m pretty sure it will be easier to program against since I can use Linq to Entities to query. It’s just a matter of figuring out how to make it work for what I need. So I’m tring to figure out Complex Types, but like everything I think I’m making it more complicated that it really is. I need to nest my complex types, but specify the column names. I’m guessing I can do that with the Fluent API via the entity that is using it as a property. See below.

public class Complaint
{
   public int ComplaintID {get; set;}
   public Location Location {get; set;}
}
[ComplexType]
public class Location
{
   public string County {get; set;}
   public decimal? Latitude {get; set;}
   public decimal? Longitude {get; set;}
   public Address Address {get; set;}
}
[ComplexType]
public class Address
{
   public string Street {get; set;}
   public string City {get; set;}
   public string ZipCode {get; set;}
}

In the context, I would define the column as such:

modelBuilder.Entity().Property(c=>c.Location.Address.Street).HasColumnName("LocationAddress");

Is this the way to handle this? If someone sees this and can tell me if/where I’m wrong, please feel free to comment. I really would love to make this work. I would like to eventually use this model instead of the edmx or sqlDataSource I’m currently using. This model would be backing 2 projects (wpf desktop app and asp.net). And once I get this model figured out, I can do the same for a few other projects I’ve got.

I will have a lot of work to do but I think it will be easier to code against once all this work is done.

About these ads

About raynebair

I write code by day, and knit and/or read by night. Somewhere in all that I'm a mother and wife too.

Share your thoughts with me

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: