background preloader

Entity Framework

Facebook Twitter

MSDN Magazine: Data Points - Demystifying Entity Framework Strategies: Loading Related Data. In last month’s Data Points column, I provided some high-level guidance for choosing a modeling workflow strategy from the Database First, Model First and Code First options. This month, I’ll cover another important choice you’ll need to make: how to retrieve related data from your database. You can use eager loading, explicit loading, lazy loading or even query projections.

This won’t be a one-time decision, however, because different scenarios in your application may require different data-loading strategies. Therefore, it’s good to be aware of each strategy so that you can choose the right one for the job. As an example, let’s say you have an application that keeps track of family pets and your model has a Family class and a Pet class with a one-to-many relationship between Family and Pet. Eager Loading in a Single Database Trip Eager loading lets you bring all of the data back from the database in one trip. From f in context.Families.Include("Pets") select f Evaluate the Pros and Cons. MSDN Magazine: Data Points - Using the Entity Framework to Reduce Network Latency to SQL Azure. At first glance, switching from a locally managed SQL Server database to the Microsoft cloud-based SQL Azure database sounds like it could be difficult. But in reality, it’s a snap: Simply switch the connection string and you’re done! As we developers love to see in these situations, it “just works.”

However, making the switch introduces network latency, which can substantially affect overall application performance. Fortunately, a good understanding of the effects of network latency leaves you in a powerful position to use the Entity Framework to reduce that impact in the context of SQL Azure. Profiling Your Data Access Code I use Visual Studio 2010 profiler tools (msdn.microsoft.com/library/ms182372) to compare different data access activities against the AdventureWorksDW database residing on both my local network and my SQL Azure account. Figure 1 Queries Designed to Explore Performance Looking at the Performance on a Local Network Switching to the SQL Azure Database All Aboard the Cloud.

Entity Framework Auditing Implementation | Soft Code Article. Extending And Customizing Code First Models – Part 2 of 2. Posted on February 15, 2013. Filed under: Entity Framework, Visual Studio | Tags: Code First, DbContext, Dynamic Model, EF, Entity Framework | Here is the scenario, your company ships a library or application that contains a Code First model for accessing the database. Your customers want to extend this model to include extra types/properties to meet their specific business needs. These types/properties will be stored in additional tables/columns in the application database. In Part 1 we looked at how to extend the model, but required the corresponding changes to be manually applied to the database.

In the second (and final) part of this series we’ll take a look at how migrations can be used by the team creating the core model, and the team extending the model. Complete source code is provided for download at the end of this post. Make sure you have read and understand Part 1 of this series before tackling this post. Using Migrations in the Core Model Introducing the Extension Points. C# - Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges() Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH) - Enterprise .Net. A simple strategy for mapping classes to database tables might be “one table for every entity persistent class.” This approach sounds simple enough and, indeed, works well until we encounter inheritance. Inheritance is such a visible structural mismatch between the object-oriented and relational worlds because object-oriented systems model both “is a” and “has a” relationships.

SQL-based models provide only "has a" relationships between entities; SQL database management systems don’t support type inheritance—and even when it’s available, it’s usually proprietary or incomplete. There are three different approaches to representing an inheritance hierarchy: Table per Hierarchy (TPH): Enable polymorphism by denormalizing the SQL schema, and utilize a type discriminator column that holds type information. Inheritance Mapping with Entity Framework Code First All of the inheritance mapping strategies that we discuss in this series will be implemented by EF Code First CTP5. The Domain Model.

Implementing Inheritance. The Contoso University sample web application demonstrates how to create ASP.NET MVC 4 applications using the Entity Framework 5 Code First and Visual Studio 2012. For information about the tutorial series, see the first tutorial in the series. You can start the tutorial series from the beginning or download a starter project for this chapter and start here. If you run into a problem you can’t resolve, download the completed chapter and try to reproduce your problem. You can generally find the solution to the problem by comparing your code to the completed code. For some common errors and how to solve them, see Errors and Workarounds. In the previous tutorial you handled concurrency exceptions. This tutorial will show you how to implement inheritance in the data model. In object-oriented programming, you can use inheritance to eliminate redundant code. Table-per-Hierarchy versus Table-per-Type Inheritance An alternative is to make the database look more like the inheritance structure.