October 24, 2010
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.
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!