Help with EF 4.1 Database First RRS feed

  • Question

  • Hi there, I'm fairly new at Silverlight, and I just changed my whole project to use EF 4.1 Database first. I'm a little lost with the whole thing. The DbContext generater creates all my POCO classed ok, but I'm having a problem with my Silverlight app seeing them because of no entity key's. "The Entity x in DomainService y does not have a key defined. Entity types exposed by DomainService operations must have at least one public property marked with the KeyAttribute.  In my model, each of
    my Entities do have a Key defined, but when my classes are automatically generated, they do not have the [Key] attribute.

    If I manually add the [Key] attribute to all of my classes, the Silverlight app builds and I can access the data. The problem is if I edit the model, and all my POCO classes are regenerated automatically, it removes the [Key], and then the Silverlight app is broken again.

    What I'm I doing wrong? All the examples on the web for Ef 4.1 seem to be for MVC apps, and none of the examples even have the [Key] attribute for the POCO classes.  Is there a way to edit the T4 template to add the [Key] attribute to my classes?

    Thanks, Scott

    Tuesday, November 1, 2011 11:11 AM


All replies

  • Hi,

    There's a few thing that very improtant does not exiting in Ef 4.1 codefirst. Because those func lives in SQL part. I might hate it.


    You can try.



    Tuesday, November 1, 2011 11:16 AM
  • Where are your T4 templates for POCO coming from? Do you really need POCO? I use with RIA Services 'normal' Entities e.g. derived from Entity and have no problem with them. As I remember, using POCOs in this scenario is not straighforward.


    I used, just to see, in existing project MS POCO template, created a new DomainService using new POCOs, no issues at all. There's more to do when I switch existing DomainService (which uses Entities) to POCOs, due to missing EntityState. 

    BTW When you you switch to POCO generator, Code Generation Strategy (property of Model) switches to None. To regenerate Entities you must click on template (*.tt file in SE) and select Run Custom Tool. In order to revert to standard Entities you can set Code Generation Strategy from None again to Default and exclude POCO templates from your project.

    Hope it helps, Greg


    I created an new Project, based on Business App template, added DB, created a table with identity and text field, code generation with POCO template - and no problem with DomainService was thrown. Even when the table had no primary key defined! Of course ADD fails but: set primary key, rebuild model and ADD works. There is no need for [Key] in POCOs...

    // <auto-generated>
    //     This code was generated from a template.
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    namespace SampleWithPOCO.Web
        public partial class TextTable
            #region Primitive Properties
            public virtual int UniqueId
            public virtual string Text

    This Entity works with DomainService.

    Wednesday, November 2, 2011 1:03 PM
  • Hi,

    The Ef assemblies missing don't truth VS

    Here is the sample https://skydrive.live.com/?sc=documents&cid=b692c2cbad70a05c#!/?cid=b692c2cbad70a05c&sc=documents&uc=1&id=B692C2CBAD70A05C%21127

    If you need to customized Poco class try using Partial class.


    The name is SilverlightApplication3


    Wednesday, November 2, 2011 6:30 PM
  • Thanks Greg, I ended up having to backout my changes in order to meet a deadline, but I will try again next week with just a sample app. thaicarrot your link does not work. 

    Thursday, November 3, 2011 11:29 AM
  • You're welcome.

    Pls find sample application here:


    just to see Entities without [Key] working

    Thursday, November 3, 2011 12:56 PM