none
Added property on an Entity Framework class not visible in silverlight client

    Question

  • Hi,

    I have generated an entity framework model from my database. In this model I have extended one class with one property:
        public partial class ProjectTask
        {
            private bool hasTimeBooked;
            public bool HasTimeBooked
            {
                get
                {
                    if (this.EmployeeProjectTasks.IsLoaded) hasTimeBooked = this.EmployeeProjectTasks.Count > 0;
                    else hasTimeBooked = this.EmployeeProjectTasks.CreateSourceQuery().Count() > 0;
                    return hasTimeBooked;
                }
            }
        }
    
    I have done this because I need to know of a projecttask has employeeprojecttasks, without reading al tasks.
    This entity model is exposed by the ado.net data service.
    public class SilverSheetDataService : DataService<SilversheetEntities>
        {
            // This method is called only once to initialize service-wide policies.
            public static void InitializeService(IDataServiceConfiguration config)
            {
                config.SetEntitySetAccessRule("*", EntitySetRights.All);
                config.UseVerboseErrors = true;
            }
        }

    After building the web application I updated the service reference in the silverlight app.
    Unfortunately my added property is not visible in the intellisense in the silverlight app.
    Inspecting the generated client classes, I saw that this property is not generated.

    Q: How can i make the added property accessable in the silverlight app?
    Thursday, June 25, 2009 11:42 AM

Answers

  • Hi pexxx, I'm sorry but you can't  do this. If you see the code that entity framework wizard generates you can see that all the properties have lots of attributes and so on.

    If you extend an entity, you will not see this property or method in the other side of the service. And, if you extend your entities in client side, be careful because the data service client library will try to serialize all public properties and you will have problems.

    Thanks!
    • Marked as answer by pexxx Friday, June 26, 2009 7:13 AM
    Thursday, June 25, 2009 3:11 PM
  • hi, i don't know if this is still relevant, but i ran into this scenario and adding the following attribute to my partial class's property exposed the property on the client side (using a normal WCF service consumed by a silverlight app):

    public partial class MyEntityClass
    {
    ...
        [global::System.Runtime.Serialization.DataMemberAttribute()] 
        public int MyProperty { get; set; }
    }
    • Marked as answer by pexxx Tuesday, July 21, 2009 2:06 PM
    Tuesday, July 21, 2009 2:04 PM

All replies

  • Hi pexxx, I'm sorry but you can't  do this. If you see the code that entity framework wizard generates you can see that all the properties have lots of attributes and so on.

    If you extend an entity, you will not see this property or method in the other side of the service. And, if you extend your entities in client side, be careful because the data service client library will try to serialize all public properties and you will have problems.

    Thanks!
    • Marked as answer by pexxx Friday, June 26, 2009 7:13 AM
    Thursday, June 25, 2009 3:11 PM
  • Hi pexxx, I'm sorry but you can't  do this. If you see the code that entity framework wizard generates you can see that all the properties have lots of attributes and so on.

    If you extend an entity, you will not see this property or method in the other side of the service. And, if you extend your entities in client side, be careful because the data service client library will try to serialize all public properties and you will have problems.

    Thanks!

    OK, Thanks.

    Is this a data service problem? Or wil I have the same problem using wcf (when i'm not building a silverlight app but a wpf app)?
    If so, how can i build a multi tier app using extended entities?
    Friday, June 26, 2009 7:16 AM
  • Hi pexxx, I think It's not a problem, you are extending a entity framework entity with a calculated property that will not be serialized because it is manually added.

    Using WCF, you define your data contract, then, you can add properties as you like.

    A litle workarond to you problem is extending entities in the client side, but you must to personalize the entities serialization, decorating it with an special attibute.

    http://blogs.msdn.com/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado-net-data-services-client-library.aspx

    Thanks :)


    P.D.: "If so, how can i build a multi tier app using extended entities?" it's a great question that I can't answer
    Friday, June 26, 2009 8:15 AM
  • I have tried the route of the [DataContract] attribute, but it didn't seem to work for me over ADO.NET Data Services Web Site.

    Have you perhaps got an example of how you do this?
    Thursday, July 02, 2009 11:21 AM
  • Excuse me Iwan, but would you be able to explain a little more your scenario, please?

    Thanks :)
    Thursday, July 02, 2009 11:26 AM
  • Thanks for the comment Vicente,

    I extended a Entity Framework Entity Object with a partial class.

    Something like:

        public partial class viewMedical_Aids
        {
    
    ...
    
            public string DateCapturedString
            {
                get
                {
                    return this.DATE_CAPTURED.DateToString();
                }
            }
    
    ...
    
         }
    viewMedicalAids is a Framework Entity which I generated (maps to a view on the database).
    DateCapturedString is a property that I've implemented (it uses an extension method DateToString) - all it does is format the date into a string.
    I then created a Dynamic Data Entities Web Site.
    Then I created another project and added a web reference to the Dynamic Data Entities Web Site.
    So now I can execute LINQ queries over the Dynamic Data Entities Web Site - the only problem is with the proxy class that gets generated - it doesn't have the DateCapturedString property. 
    I figured, since it's a WCF site, I must add the [DataContract] attribute to the property.  I did that, updated the web reference, but still couldn't get it to work.

    Am I doing something wrong or is this not possible?

    :)
    Thursday, July 02, 2009 1:42 PM
  • hi, i don't know if this is still relevant, but i ran into this scenario and adding the following attribute to my partial class's property exposed the property on the client side (using a normal WCF service consumed by a silverlight app):

    public partial class MyEntityClass
    {
    ...
        [global::System.Runtime.Serialization.DataMemberAttribute()] 
        public int MyProperty { get; set; }
    }
    • Marked as answer by pexxx Tuesday, July 21, 2009 2:06 PM
    Tuesday, July 21, 2009 2:04 PM
  • hi, i don't know if this is still relevant, but i ran into this scenario and adding the following attribute to my partial class's property exposed the property on the client side (using a normal WCF service consumed by a silverlight app):

    public partial class MyEntityClass
    
    {
    
    ...
    
        [global::System.Runtime.Serialization.DataMemberAttribute()] 
    
        public int MyProperty { get; set; }
    
    }
    
    

    At this moment it's not relevant anymore. But I keep your solution in mind, may I ran in the same problem again.
    Thanks
    Tuesday, July 21, 2009 2:07 PM
  • Hi,

    adding the attribute does not work... I want to add a custom property SERVER-Side...

    Any suggestions (VS2008, FRamework 3.5 SP1)?

     

    THx

    Friday, August 20, 2010 1:54 PM
  • I was asking the same question on the other thread.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/80cc8037-ecac-4922-b64b-5760727f201b

    So the answer seems no to my question. We currently can't add a custom field to the Entity class and have it exposed to client via WCF Data service. 

    But we can easily do it in RIA service or plain WCF service, and it's a very common practice to add extra calculated fields to Entity and filled data on server.  I don't see why WCF data service can't expose them.  I hope WCF Data service group address this issue in the future release. 

    I choose WCF DataService over RIA service for my application because I thought RIA service is too rigid. Now I'm finding WCF DataService has it own way of being too rigid.

    The way I finally solved my problem was to add extra fields in the DB table so they got into the Entity Model, even I leave them empty in DB and filled them in code.  I did this just for the purpose to expose those fields while still using WCF Data Service. What a hack!

     

     

     

     

     

    Friday, August 20, 2010 5:55 PM