Browse by Tags

Tagged Content List
  • Blog Post: Tip 56 - Writing an OData Service using the Reflection Provider

    At TechEd I got a lot of questions about how to expose Data as OData By now you probably know you can use Data Services and the Entity Framework to expose data from a database as an OData Service. You might even know you can use Data Services with a custom Data Service Provider to expose arbitrary...
  • 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: Tip 54 – How to improve performance using Statement Expressions

    Background: While writing the update post in my Data Service Provider series I ended up writing this block of reflection code to copy properties values from one object to another: foreach (var prop in resourceType .Properties .Where(p => (p.Kind & ResourcePropertyKind.Key) != ResourcePropertyKind...
  • Blog Post: Tip 53 – How to debug EF POCO mapping issues

    If you are trying to use POCO classes in EF 4.0, it is relatively easy to run into problems with the mapping from your model to your CLR classes. If you get any nasty problems here, the best way to get to the bottom of things is to try to explicitly load the metadata for your POCO types. For example...
  • Blog Post: Tip 52 – How to re-use Types with the Data Services client

    By default when you add a Data Service Service Reference you get automatic code-generation, which produces a strongly typed DataServiceContext and classes for all your ResourceTypes. You can have a look at this generated code if you ‘show all files’ in your project: And then expand your Data...
  • 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: Tip 50 – How to query a Data Service using JQuery

    Recently I’ve been spending some of my time playing with JQuery . And because Data Services can expose data in JSON format, I thought I’d use JQuery to grab some data from a Data Service. Turns out it is pretty simple. This example isn’t going to win any beauty awards, but it will show you the...
  • Blog Post: Tip 49 – How to find your Data Service bug

    I’ve been playing around with creating a custom Astoria Data Service Provider , aka DSP, for a while now. So far I’d managed to get my metadata all setup, so browsing to $metadata works great. I’d got a few simple queries work. Basically everything was going fine. At least until I tried this URL: I wasn...
  • 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 44 – How to navigate an OData compliant service

    I recently did a crash course in Data Services and OData . While doing so I realized my notes might be useful for you guys. So here is my little cheat sheet to quickly get up to speed with OData Urls. Note: OData Services may not necessarily support all of the following features: but if they do, this...
  • Blog Post: Tip 43 – How to authenticate against a Data Service

    Problem: When writing code against a Data Service, like say SharePoint , the client application must provide a valid set of credentials, or you will see a dreaded “401 Unathorized” response. For Silverlight applications hosted on the same site as the DataService, this is generally handled for you...
  • 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: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • 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...
Page 1 of 3 (58 items) 123