locked
Computed Properties in WCF Data Services 5.6.x? RRS feed

  • Question

  • Hello Community,

    We're looking to add a computed property to an entity that is exposed via an EntityFramework Code-First WCF Data Service(via DbContext), and we're having a heck of a time figuring out how to do it.  There doesn't seem to be any resources on this.

    Here is the closest thing we could find, but it's rather dated and seems to be inaccurate (that is, simply using CodeFirst doesn't work):

    http://stackoverflow.com/questions/3863113/how-to-create-a-computed-property-in-data-services-odata

    Can someone point us in the direction on the best way to do this with the latest WCF Data Services (5.6.x)?

    Thank you for any assistance you can lend!

    Michael 

    Friday, March 28, 2014 11:39 PM

Answers

  • Hello,

    >>but it's rather dated and seems to be inaccurate (that is, simply using CodeFirst doesn't work):

    Being confused about why it does not work. Here I just a made a small example and it could query and add successfully with the computed property:

    The model:

    public class S31Context : DbContext
    
        {
    
            public S31Context()
    
            { }
    
            public DbSet<Person> person { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Person>().Property(p => p.PersonID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
            }
    
        }
    
    
        public class Person
    
        {
    
            public int PersonID { get; set; }
    
            public String FirstName { get; set; }
    
            public String LastName { get; set; }
    
            public String FullName
    
            {
    
                get { return FirstName + " " + LastName; }
    
                set { }
    
            }
    
        }
    

    Call it in client:

    S31Context db = new S31Context(new Uri("http://localhost:5020/WcfDataService1.svc/"));
    
    
                //Query
    
                //ServiceReference1.Person p = db.person.FirstOrDefault();
    
    
                //Add a new Person into dartaabse
    
                Person p = new Person() { PersonID = 2, FirstName = "A", LastName = "B" };
    
    
                db.AddToperson(p);
    
    
                db.SaveChanges();
    

    The computed property will automatically contract the FirtsName and LastName as a new value and save into database

    You can have a try.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Mike-E_wins Monday, March 31, 2014 8:35 PM
    Monday, March 31, 2014 2:24 AM
    Moderator

All replies

  • Hello,

    >>but it's rather dated and seems to be inaccurate (that is, simply using CodeFirst doesn't work):

    Being confused about why it does not work. Here I just a made a small example and it could query and add successfully with the computed property:

    The model:

    public class S31Context : DbContext
    
        {
    
            public S31Context()
    
            { }
    
            public DbSet<Person> person { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Person>().Property(p => p.PersonID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
            }
    
        }
    
    
        public class Person
    
        {
    
            public int PersonID { get; set; }
    
            public String FirstName { get; set; }
    
            public String LastName { get; set; }
    
            public String FullName
    
            {
    
                get { return FirstName + " " + LastName; }
    
                set { }
    
            }
    
        }
    

    Call it in client:

    S31Context db = new S31Context(new Uri("http://localhost:5020/WcfDataService1.svc/"));
    
    
                //Query
    
                //ServiceReference1.Person p = db.person.FirstOrDefault();
    
    
                //Add a new Person into dartaabse
    
                Person p = new Person() { PersonID = 2, FirstName = "A", LastName = "B" };
    
    
                db.AddToperson(p);
    
    
                db.SaveChanges();
    

    The computed property will automatically contract the FirtsName and LastName as a new value and save into database

    You can have a try.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Mike-E_wins Monday, March 31, 2014 8:35 PM
    Monday, March 31, 2014 2:24 AM
    Moderator
  • OK, that works.  Thank you, Fred.  Having an empty setter does seem unnatural and hacky, not to mention the database is now storing inaccurate (and unnecessary) data.  Nonetheless, it's the best/fastest/easiest solution to achieve what we are after.



    Monday, March 31, 2014 6:33 PM