Browse by Tags

Tagged Content List
  • Blog Post: Tip 55 - How to extend an IQueryable by wrapping it.

    Over the last couple of years I’ve found myself in lots of situations where I’ve wanted to get ‘under the hood’ and see what is happening inside an IQueryable, but I haven’t had an easy solution, at least until now. Getting down and dirty like this is interesting because it means you can: Log...
  • Blog Post: Creating a Data Service Provider – Part 8 - Relationships

    In parts 1 thru 7 we made a Read / Write typed data service provider, although it was a little simplistic because it didn’t have relationships. So lets rectify that right now by adding a relationship to make this more 'real-world' Changes to our Classes If you remember from Part 3 we had just a Product...
  • Blog Post: Tip 51 – How to load EF metadata from arbitrary streams

    In Tip 45 I showed you how to build a connection string at runtime, which is pretty nifty. The problem with that was that it relies on having metadata files (.csdl .ssdl and .msl) on local disk. But what if they live on a web-server or something and you don’t even have access to the local file...
  • Blog Post: Custom Data Service Providers

    Introduction Data Services sits above a Data Service Provider, which is responsible for interacting with the underlying Data Source on behalf of the Data Service. Data Services ships with some internal providers, and makes it possible for you to create custom providers too. So the obvious question...
  • Blog Post: Tip 47 – How fix-up can make it hard to change relationships

    Problem: Take this code: Category oldCategory = ctx.Categories .Include("Products") .First(c => c.Name == "Drink"); Category newCategory = new Category {Name = "Beverage"}; foreach(Product product in oldCategory.Products) { newCategory.Products.Add(product); } Ignore for a second that in...
  • Blog Post: Tip 46 – How to exclude a property using Code-Only

    This time a real simple one prompted by this question on StackOverflow. Problem: If you tell the Entity Framework about this class using Code-Only, by default every property becomes part of Entity, and as a result stored in the database. Usually this is what you want. But not always, imagine this class...
  • Blog Post: Tip 45 – How to swap EF metadata at runtime.

    Background By default the Entity Framework embeds its metadata inside your assembly as a resource. It also puts a connection string in the App or Web Config that references those resources something like this: <add name="BloggingEntities" connectionString="metadata=res://*/Blogging.csdl|res://*/Blogging...
  • Blog Post: Tip 42 – How to create a dynamic model using Code-Only

    Background: When we give examples of how to use Code-Only we always start with a strongly typed Context derived from ObjectContext . This class is used to bootstrap the model. For example this (property bodies omitted for simplicity sake): public class MyContext : ObjectContext { public ObjectSet<Category>...
  • Blog Post: Tip 41 – How to execute T-SQL directly against the database

    Sometimes you’ll find you need to issue a query or command that the Entity Framework can’t support. In fact this problem is common to most ORMs, which is why so many of them have a backdoor to the database. The Entity Framework has a backdoor too… .NET 3.5 SP1 In .NET 3.5 SP1 you can get to the underlying...
  • Blog Post: 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; set; } public virtual List<Product> Products...
  • Blog Post: 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;...
  • Blog Post: 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...
  • Blog Post: 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 different...
  • Blog Post: Tip 32 – How to create a database from SSDL – EF 4 only

    We recently released a CTP that extends EF 4 Beta 1 which included Code Only. You can read more about Code Only here , here and here . If you look at the walkthroughs for Code Only you will see code that looks something like this: // Create a builder and configure it var builder = new ContextBuilder<MyContext>...
  • Blog Post: Tip 31 – How to compose L2O and L2E queries

    Imagine you want to write a query like this: var possibleBuyers= from p in ctx.People where p.Address.City == “Sammamish” && InMarketForAHouse(p) select p; Now theoretically this is possible so long as there is a SQL translation for InMarketForAHouse. In EF 4.0 you do this by creating a CLR stub...
  • Blog Post: Tip 30 – How to use a custom database function

    Imagine you have a database function like the DistanceBetween function in Nerd Dinner : CREATE FUNCTION [dbo].[DistanceBetween]( @Lat1 as real, @Long1 as real, @Lat2 as real, @Long2 as real) RETURNS real AS BEGIN … END And you want to use it with the Entity Framework. Declaring the Function...
  • Blog Post: Tip 27 – How to Implement BeforeSave Validation

    It is common to want to validate that your entities are ‘valid’ before you save them to the database. A naive form of validation might be to try to ensure that your entities are ‘valid’ before you add them to the context, but this doesn’t help you if you pass through ‘invalid’ states, i.e. as you build...
  • Blog Post: New EF blogger – Craig Lee

    Short one this time. Craig Lee , a colleague of mine who works on the tools team, has just started blogging. He already has a number of posts, including an interesting one on keyboard shortcuts . Enjoy.
  • Blog Post: Tip 26 – How to avoid database queries using Stub Entities

    What are Stub Entities? A stub entity is a partially populated entity that stands in for the real thing. For example this: Category c = new Category {ID = 5}; is a stub entity. It has only the ID populated, which indicates this is a stub for Category 5. When are Stub Entities Useful...
  • Blog Post: Wrapping Providers

    For well over a year now I've been talking about how it is possible with EF to write a provider that sits between the EF and the native database provider. There are lots of reasons why you might do this. Here are some examples, you could write a: Tracing Provider Auditing Provider Caching Provider Query...
  • Blog Post: Tip 25 – How to Get Entities by key the easy way

    Sometimes rather than writing this: var customer = ctx.Customers.First(c => c.ID == 5); You would rather write something like this: var customer = ctx.Customers.GetCustomerById(5); In .NET 4.0 this would be trivial to do by modifying the T4 templates that do code gen. You would simply...
  • Blog Post: Tip 22 - How to make Include really Include

    This is 22nd post in my ongoing series of Entity Framework Tips . If you want to do eager loading with the Entity Framework it is generally really easy, you simply write something like this: var results = from post in ctx.Posts.Include(“Comments”) where post.Author.EmailAddress == “alexj@microsoft...
  • Blog Post: Tip 21 – How to use the Single() operator – EF 4.0 only

    This is 21st post in my ongoing series of Entity Framework Tips , and the first that is specific to EF 4.0. Entity Framework 4.0 Beta 1 As you’ve probably heard VS 2010 Beta 1 is now available to subscribers which means some of you can get your hands on EF 4.0 Beta 1 too. If you have any questions our...
  • Blog Post: Tip 20 – How to deal with Fixed Length Keys

    This is 20th post in my ongoing series of Entity Framework Tips . Fixed Length Field Padding: If you have an fixed length column in the database, for example something like NCHAR(10) when you do an insert, padding happens automatically. So for example if you insert ‘12345’ you get 5 spaces automatically...
  • Blog Post: Tip 19 – How to use Optimistic Concurrency with the Entity Framework

    This is the 19th post in my ongoing series of Entity Framework Tips . Background: If you have a table with a timestamp column, and you reverse engineer an Entity from that table, you will end up with a Binary property in your entity (in my example called Version). If you look at the properties...
Page 1 of 2 (40 items) 12