Tip 40 – How to materialize presentation models via L2E

Problem: Imagine that you have these entities public class Product {     public int ID { get; set; }     public string Name { get; set; }     public virtual Category Category { get; set; } } public class Category {     public int ID { get; set; }     public string Name { get;…

5

Tip 39 – How to set overlapping Relationships – EF 4.0 only

Scenario: In EF 4 we have FK relationships, available for the first time in .NET 4.0 Beta 2, so it is now possible to have a model something like this: public class Division {    public int DivisionID {get;set} // Primary Key    public string Name {get;set;}    public virtual List<Lawyer> Lawyers {get;set;}    public…

1

Tip 38 – How to use CodeOnly with Astoria

The normal way that you create an ADO.NET Data Services (aka Astoria) Service is by creating a class that derives from DataService<T>. public class BloggingService : DataService<BloggingEntities> And if you want to use Entity Framework under the hood the T you supply must derive from ObjectContext. Now this works great most of the time, but…

0

Code-Only best practices

There have been lots of posts on the EFDesign blog talking about how the features in Code Only have evolved. But very little covering what we think will be the best way to write the code. You may have seen code like this: var prodConf = builder.Entity<Product>(); productConf.HasKey(p => p.Id); productConf.Property(p => p.Id).IsIdentity(); productConf.Property(p =>…

3

Tip 37 – How to do a Conditional Include

Problem Someone asked how to do a Conditional Include a couple of days ago on StackOverflow. They wanted to query for some entity (lets say Movies) and eager load some related items (lets say Reviews) but only if the reviews match some criteria (i.e. Review.Stars == 5). Unfortunately though this isn’t strictly supported by EF’s…

13

Tagging File Systems?

I had an interesting in passing conversation with someone today about tagging file systems… which reminded me of my own sordid past. Before I joined Microsoft, before I was an MVP, before I released Base4, I wrote something called XTend. At the time I called it a relational file system, but this was back in…

6

Tip 36 – How to Construct by Query

While writing my tips series and writing EF controllers for MVC I found that I regularly wanted to create and attach a stub entity. Unfortunately it isn’t quite that simple, you have to make sure the entity isn’t already attached first, otherwise you’ll see some nasty exceptions. To avoid these exceptions I often found myself…

6

Tip 35 – How to write OfTypeOnly<TEntity>()

If you write a LINQ to Entities query like this: var results = from c in ctx.Vehicles.OfType<Car>()               select c; It will bring back, Cars and any type that derives from Car, like say SportCar or SUV. If you just want Cars and you don’t want derived types like SportCars or SUVs in LINQ to…

4

Tip 34 – How to work with Updatable Views

UPDATE: thanks Zeeshan for pointing out that by default only non-nullable columns end up in the key for view backed entities. Imagine this situation, you have a view in your database, and it is updatable. Next you decide to use this view with the Entity Framework, so you go ahead and import it. The resulting…

16

Tip 33 – How cascade delete really works in EF

Imagine that in your database you have a cascade delete on an FK relationship. Something like this: Here the Delete Rule says that when a Category is deleted all the related Products should be deleted too. If you generate an EF model from this database you get a model that on the surface looks no…

6