Browse by Tags

Tagged Content List
  • 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 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 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 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 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 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 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 24 – How to get the ObjectContext from an Entity

    Customers often ask how to get from an Entity back to the ObjectContext . Now generally we don’t recommend trying this. But sometimes you really need a way to get to the ObjectContext . For example if you are in method and all you have is the Entity, and you need the ObjectContext perhaps to do...
  • Blog Post: Tip 23 – How to fake Enums in EF 4

    As of right now Enums are not in EF4. Now we will be listening to your feedback about Beta1, and making some adjustments, so you never know, but at the moment it doesn’t look like they will be supported. Yesterday though I came up with a workaround that, while a bit of work, is pretty interesting...
  • Blog Post: Ordering of Commands – What do you think?

    Some of our customers write code like this: ctx.AddToCustomers(customer); ctx.AddToProducts(product1); ctx.AddToProducts(product2); And expect the Entity Framework to issue 3 insert commands in the same order. Today the EF doesn’t preserve this sort of ordering. Instead the order inserts/updates and...
  • Blog Post: Learn about EF Pluralization Services courtesy of Dan Rigsby

    Not long ago I wrote a ‘sneak peek’ for our new Pluralization features on the ADO.NET team blog. There is a more indepth post coming soon… But it seems the community is starting to do our education for us, and beating us (or should I say me) to the punch! Dan Rigsby , who is a WCF MVP, has a fantastic...
  • 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: Tips and Tricks

    Hopefully if you're reading this you've noticed that I've started a series of Tips recently. The tips will mostly apply to Entity Framework or Data Services. Seeing as I expect to have a lots of tips, it probably makes sense to have some sort of index. That is what this is post is, as I add...
Page 1 of 1 (16 items)