The Wordy Bit

The diligent reader may have noticed that we skipped certain things:

  1. The AuthorView form has no way of deleting an author. Because the author has associations with other tables, you'd need to ensure that a deleted author deletes everything else. This is now left as an exercise to you. It's simply a matter of creating a DeleteAuthor stored procedure and mapping it to the Author entity. This task will also show you a quirk of the framework—you cannot map the Delete function on its own. You must create stored procedures for Insert, Update, and Delete and map them all together.
  2. The ArticleView form has no way of actually associating an author with an article. You may want to change the author of the article or set the author as nobody or just simply look at who the author is. This could be accomplished in three ways.
    1. As shown earlier, using a method expression: publishContext.Article.Include("Author");
    2. For each article entity, perform a currentArticle.AuthorReference.Load(). This will perform a SELECT against the database. This, too, is left as an exercise to you.
    3. Have a stored procedure return all the details for the article and the corresponding author and map it to an Article entity and Author entity. This is a topic in the ADO.NET Entity Framework known as Complex Types, which you shall explore in another tutorial.


Now that you've seen the code and gone through basic ADO.NET Entity Framework operations, I can get into the digressive harangue. I can already see your fingers hovering over the Ctrl and F4 buttons, but I would strongly recommend having a quick read through this to get a better understanding of the framework for future work and how you could use it in your application(s).
More Information about the Entity Framework

There are a few topics that you have already touched upon in this tutorial; you will go into more detail for each of these:

  1. What is the ADO.NET Entity Framework?
  2. How is it different from LINQ to SQL?
  3. Querying data with LINQ to Entity expressions and Method Expressions
  4. Complex Types and Association Sets


What is the ADO.NET Entity Framework?

The Entity Framework is Microsoft's first Object-Relational Mapping (ORM) framework for the .NET Framework. An ORM framework allows developers to work with a conceptual schema of a database. A conceptual schema is quite simply the logical representation of objects in a database. In other words, it is a representation of a set of concepts (Forum Members, Threads, Posts) and relationships (one to one, many to one).

For instance, in your database, if you have a 'link' table that allows for a many-to-many mapping between two other tables, the 'link' table is not an entity in the EDM. It is simply a relationship, but the two main tables that it is linking may be entities. Further, you may have a table that has been horizontally split for some reason; the two tables together map to a single entity.

When using the framework, you can refer to a Customer entity's Address property (Customer1.Address.PostCode), with the Address actually coming from some other table.

The Entity Framework abstracts the groundwork required to map the application classes to each other by generating and understanding the relationships between each of them (among other things) so that you do not need to perform database-plumbing in your code. You simply work with the objects in a relational model and remain 'unaware' of the underlying database implementation.

How Is It Different from LINQ to SQL?

A commonly asked question.

  • LINQ to SQL was created with rapid application development (RAD) in mind. The Entity Framework was created with enterprise development in mind.
  • LINQ to SQL works with the objects in a database. The Entity Framework works with the conceptual model of a database. As explained earlier, these are two different things. This further means that the Entity Framework allows you to perform queries against relationships between entities, mapping single entities to multiple tables, and so on.
  • LINQ to SQL has the ability to call stored procedures, but not map them the results. The Entity Framework is all about mapping results to entities.
  • Guess which one works with something besides SQL Server?


Querying Data with LINQ to Entity expressions and Method Expressions

Throughout the tutorial, you saw a spattering of LINQ to Entities expressions and Query Builder methods or Method Expressions. These are the basic expressions used to query the entities in the EDM. Internally, both of them evaluate to the same thing, so using one over the other is a matter of preference.

For example, this query method:

vb Code:
  1. Dim articleQuery As ObjectQuery(Of Article) = publishContext.Article.Where("it.ArticleID > 5")

can be represented as

vb Code:
  1. Dim articleQuery As IQueryable(Of Article) = From ar in publishContext.Article _
  2.                                                             Where ar.articleId > 5 _
  3.                                                             Select ar

Although LINQ to Entities may be more familiar to you, take a short look at query methods. Query methods allow you to parametrize the queries going through. Taking the above example, you therefore can do this:

vb Code:
  1. Dim articleQuery As ObjectQuery(Of Article) = publishContext.Article.Where("it.ArticleID > @SomeArticleID", new ObjectParameter("SomeArticleID", 5))

Going back to one of the forms you worked on—the PayrollView—you could have modified a query slightly to load the author's first name and last name together using the Select method.

vb Code:
  1. 'Uses "ADO.NET Entity SQL Expressions" or "method based queries"
  2. authorList.DataSource = publishContext.Author.Select("it.FirstName + ' ' + it.LastName As FullName, it.AuthorID")
  3. authorList.DisplayMember = "FullName"
  4. authorList.ValueMember = "AuthorID"

Other more commonly used methods are OrderBy, GroupBy, and Where. For more information on these methods, visit the Query Builder Methods page on MSDN.

Complex Types and Association Sets

One of the scenarios described earlier in this tutorial potentially called for the use of a ComplexType. You could have created a CAuthor type, and assigned it as a property of a CArticle type, which would have enabled you to work with them more efficiently for your purposes. Or, to look at it another way, think of a Person entity that has the properties PhoneCountryCode, PhoneAreaCode, PhoneNumber, PhoneExtension. Using a ComplexType, you could group those fields into a single Phone entity. Read more about Complex Type Objects.

Association Sets allow you to use and refer to entities as properties of other entities in the Entity Framework. Because the EDM generated will not always have the association that you want there, you will sometimes need to define your own association sets between entities and also use stored procedures to perform INSERTs or DELETEs across them.

Conclusion

This has been a quick look at the ADO.NET Entity Framework. So far, you have seen how to perform a few basic operations of the ADO.NET Entity Framework.

  • LINQ to Entities to retrieve data
  • Method expressions to retrieve data
  • SaveChanges()
  • Stored procedures to retrieve data
  • Stored procedures to add, update, and delete data.


Even though there is a lot more to the Entity Framework, what you have learned here can serve as a basis for future application design. A lot of us are (subconsciously) familiar with ORM but have not used it to its full potential, so you may find yourself finding your steps initially as you wrap your head around a slightly different concept of creating data-driven applications.


Hopefully, this tutorial was useful to you in some way. Feel free to post in the VB.NET/Database Development/C# forum if you have any further questions about this.



Keywords
ADO.NET
Entity Framework
EF
mendhak
introduction
tutorial
VB.NET
C#