Entity Framework Part 5 Model First : Create a database with the Complex Types.

A cool feature we are gonna try out in the model first architecture of the entity framework are “Complex Types” a container for property values who are not entities, but a collection of fields that are in line in a table row, a set of columns or properties and not necessary tables itself, to keep a nice clean application based model and have your attributes grouped together.

Model First and Complex Types add new features and possibilities:

We are going to build a console app with Entityframework 4 and name it MyShoeShoes. First we add a new item and choose the ADO.NET Data Model. We choose the empty model:

We call the Model shoe.edmx and now we are going to add our first new entity called: ‘Shoe’ in the Model Browser

Right click-> add a complex type instead of table rows. With Complex Types you can group together specific collections. In the Model browser you see the Complex type, here we add new lines, Add -> Scaler Property- we add extra shoe properties, in this case a group of ShoeTypes; Heels, Sneakers, Boots etc.

Now we have created Complex Types that do not show up in the design surface, that is for entities only.

Now all we have to do is right click the Shoe entity and choose ‘add Complex Property’ and name it ShoeType and it automatically sets it to the complex type we’ve just created: ShoeType.

Now we are going to add a hierarchy, which inherits from the Shoe entity with some extra details we want to use with the Shoe entity. We create an entity and set the base type to Shoe. Now remember this model is a ConceptualEntityModel and we can always tweak this in the database later!

Next entity we create is the ShoeDescription and add an association to it on Shoe, so it links to the Shoe table.

Our model is ready: We have Shoe with a grouped ShoeType, so we can index our ShoeTypes on Type-a-Shoe, We created a Shoe Colour Hierarchy to Shoe, Since one ShoeType can have many colours and a table ShoeDescription always has a relation in Shoe.

Maybe you’ve noticed that our console app gives an error when you try to built the app: “Error 11007:Entity type ‘EntityName’ is not mapped when Entity is only mapped to function import” No worries! this error will be solved as soon as we build our database, as the database is generated it will create the mappings for you.

We have all the data we need for our application so it’s time to generate a database from this model

The next steps are very easy, we are asked to choose a data connection, don’t get distracted by the Shoe.dbo line, we choose a “New Connection” since we have built a brand new Model to create a database from. The connection string is created here as well.

Set the connection Properties and name the new database, we call it MyShoeShoes:

The next step generates a dll file, but this is not run automatically,  we need to create the tables in our database. The database dll will be generated by a so called T4 template and can be modified.

When we look into the generated DLL, we see that the associations are created and it sets the ID as primary key, We call this Table per type, it creates the hierarchy Shoecolor as it’s own table with a Shoe_ prefix: ‘Shoe_Shoecolor’ in the database.

We have to manually run this query in the SQLServer, since the RTM is not completed in VS2010 EF4, but this is just a matter of time. Now refresh the MySHoeSHoes database and pop the tables are generated:

We have now created a complete database from a Model we built in Visual Studio with EF4, how cool is that!

Entity Framework from scratch Part 4 EF4 Multi Layered – System.Data.Entity not referenced

As i mentioned in the previous post, there seems to be a problem when you work with Multi layered Solutions in the Entity Framework. You need to add the Entity references to all the solutions in your project, but i found another small problem when creating a web front to your application.
I’ve built this app with EF4 features with web functionality.

When i debug my code, the database connection works and the table rows are fetched, but my webpage displays an compilation error: CS0012: The type ‘System.Data.Objects.DataClasses.EntityObject’ is defined in an assembly that is not referenced. You must add a reference assembly to ‘System.Data.Entity, version=, Culture=neutral, PublicKeyToken=b77a5c561934e089’.

You need to add extra code manualy to your Web.config file directly under the system.web


copy/paste the following code:

<compilation debug="true" targetFramework="4.0">
<add assembly="System.Web.Abstractions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Data.Entity, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

If you built and re-run the the app, the webpage is displayed correctly.