Creating Entity Data Model Without A Database With Entity Framework 4
This article shows you how to generate an Entity Data Model for Entity Framework 4 using Visual Studio 2010. Entity Data Model is widely known by its acronym EDM. A common misconception among developers is that a EDM is always backed up by a database. This is not accurate as we will see in this article. A database is a storage medium to store the data processed via EDM. Database schemas could also be different from EDM and this difference is resolved by using mapping concepts when the need comes to persist or load data from a physical database.
For this article we will create a model for a fictitious bank called MyBank. To get started we will create a project of type Class Library in Visual Studio 2010. Next we will add a new item of type ADO.NET Entity Data Model

Entity Data Model wizard then starts and on the first window of the wizard we are given a choice of either generating our model from an existing database or creating a blank model. For this article we will go with the later. Once we click Finish, our blank model is generated for us. As our model is blank, we will need to create some entities within our model for our Bank. Let’s start by creating a customer entity. By this time our project also contains a MyBank.edmx file and if it is not open then we can open it by double clicking on the file. This opens up the Entity Model Designer where we will create our entities. So the customer entity first.
To create our customer entity we right-click on and empty area in the designer and select Entity…

This brings up the Add Entity dialog box where we will enter basic details such as Entity Name, Base type which will be none in this case and Entity set. We will also check the Create key property. It indicates that our customer entity will have a property which will be its unique identifier. We can go with the name “Id” or supply our own name. We will stick with “Id”.

After clicking OK we see our newly created entity in the designer.

At this point customer entity can only store Id for customer. Storing just an Id for a customer is a bit useless regardless of the bank being fictional or not. We will add some more fields to store other information such as name, date of birth etc. To do this we can right-click on customer entity and then click Add –> Scalar Property. Scalar properties in EDM are used to store what I call base-level information which in this case will be first name, last name and date of birth. Navigation Properties are used to store relationships with other entities for example a customer could have accounts in which case we could put a Accounts navigation property on customer entity. For now we will go with scalar property.

We will add three properties here FirstName, LastName and DateOfBirth.

By default all our newly added properties store data of type string. This is okay for FirstName and LastName but won’t work well for DateOfBirth. We will now change the data type for DateOfBirth. Best way to do this is by opening Model Browser window and then selecting a property and modifying its properties. Sounds a bit confusing? Don’t worry it’s very simple. To make our life easy we can pin Model Browser and Properties window so that they are both viewable at the same time.

Here by selecting DateOfBirth property in Model Browser we can change its data type in properties window.

We will now create another entity called Account. We will follow similar steps as we did while creating Customer entity. In Account entity we will create x fields
- Id: Primary identifier of Account
- AccountNumber: Used to store the actual account number.
- BranchNumber: Stores a code which indicates the branch where this account was opened.
- CurrentBalance: Obvious enough.
- CreatedDate: Obvious enough.
Now we should have two entities in our EDM, a Customer entity and an Account entity. Let’s say that a customer can have more than one account. To reflect this properly in our EDM we will create an Association. To create an Association we right-click on the designer then click Add –> Association…

We are now presented with a dialog where we can supply information for this association.

Because we only have two entities within our designer, Visual Studio has acted clever and read our mind. It has already filled in all the information we need to create this association. We will quickly go through some of what’s already in the dialog box. First thing we have is the name for Association, second and more important is the two end-points it has created (note: this has nothing to do with WCF endpoints). First end point describes the relationship from Customer’s side and the second end point describes the relationship from Account’s side. In Multiplicity drop-down we can see that there are many accounts for a customer. We will now click OK.
Our entities now indicate visually that a one to many relationship exists between customer and account. We can also see that Navigation properties have also been added to both Account and Customer entities.

We have successfully created an Entity Data Model for a fictitious bank which stores information about customers and their accounts. This EDM does not store information in a database. However it is fully functional EDM and we can use it within an application of course the data we create will not be persisted anywhere. In a future article we will see how to generate a physical database form this Entity Data Model.







Can we generate the database script with this data model?
Yes we can. I’m putting together a post to demonstrate it. It will be up by tomorrow.
[...] an earlier post we looked at how to generate an Entity Data Model using Visual Studio 2010. The data model we [...]
When i create the association, Visual studio 2010 doesn’t create me automatically the foreign key (like the ‘CustomerId’ property here).
What should i do ?
Thanks a lot.
fennoo,
The first thing I’ll check if that the relationship exists in database.