locked
CodeFirst(?) Computed Column RRS feed

  • Question

  • User182634413 posted

    Hello, How can I get Dynamic Data to generate a ReadOnly field/column for my Entity? I've seen many folks give the example of a "Total" column that would take Quantity * Price and total it up. The general advice seems to be to "Put it in the database" - so it gets transfered to the EntityFramework model.. The thing is, what if.. I want to get the data from somewhere else? For example I have a "IT_Server" class that has a computed property of "IsServerOn" and I want to run some code to determine that by.. say pinging the server quickly..

    So I'd do something like this:

    partial class IT_Servers : IAuditable
    {
         public class Metadata
         {
              [ScaffoldColumn(false)]
              public object Id;
    
              public object ServerName;
    
              [ScaffoldColumn(true)]
              public object IsServerOn;
         }
    
         [LazyLoad] // lazy loading would be neat to add later :)
         public bool IsServerOn
         {
              get
              {
                   return VirtualServerSDK.GetPowerState(ServerName);
              }
         }
    }

    How do I get it to show on the auto generated List and Edit pages?

    Tuesday, April 10, 2012 6:40 PM

Answers

  • User3866881 posted

    Hello:)

    【Solutions】

    You should open SQL Management Studio first and set the default value for the totalColumn。And then for the Total property,plz just mark it with the data annotation——

    1)[DataBaseGenerateOption(DataBaseGenerateOption.Computed)] (Code first)

    2)For db-first,just open your edmx file and then create StoreGeneratedPattern="Identity" for you Total property。

    Reguards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 11, 2012 9:25 PM