none
Entity Framework Determine Computed Columns RRS feed

  • Question

  • I populate a DataTable with column headers from my EF and then perform a CSV read and data manipulation into the various columns in the DataTable.

    When I go to Write the data back to the database, I get an error because it maps my column names automatically and includes the Computed Columns.

    I populate the DataTable by using the code

    var properties = typeof(T).GetProperties();
    
                foreach (var property in properties)
                {
                    dt.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);
                }

    Where (T) is my EF.

    Is there a way to exclude the computed columns in the code above?

    Regards

    Thursday, November 30, 2017 5:41 AM

Answers

  • I managed to solve this by creating a new entity and making my datatable enumerable.

    I can then iterate through the datatable and map to the Entity.

    Thanks for your help.

    Friday, December 1, 2017 3:19 AM

All replies

  • Please show us the Database table and the EF class representing the table, nortmally EF tales care of computed columns correctly


    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.

    Thursday, November 30, 2017 10:38 AM
  • Database Table

    Class

     public partial class MWDHeader
        {
            public string MWDH_Holeid { get; set; }
            public int MWDH_Version { get; set; }
            public string MWDH_HoleId_Filename { get; set; }
            public string MWDH_HoleId_GPS { get; set; }
            public Nullable<decimal> MWDH_GPSLatitude { get; set; }
            public Nullable<decimal> MWDH_GPSLongitude { get; set; }
            public string MWDH_RigID { get; set; }
            public string MWDH_DrillerId { get; set; }
            public string MWDH_HammerType { get; set; }
            public Nullable<decimal> MWDH_DepthReached { get; set; }
            public Nullable<System.DateTime> MWDH_StartDrillDateTime { get; set; }
            public Nullable<int> MWDH_StartDrillTimeUnix { get; set; }
            public Nullable<System.DateTime> MWDH_EndDrillDateTime { get; set; }
            public Nullable<int> MWDH_EndDrillTimeUnix { get; set; }
            public Nullable<decimal> MWDH_DrillTime { get; set; }
            public Nullable<System.DateTime> MWDH_FileDate { get; set; }
            public Nullable<System.DateTime> MWDH_LoadDate { get; set; }
            public string MWDH_PulldownMeasure { get; set; }
            public string MWDH_HoldBackMeasure { get; set; }
            public string MWDH_StringWeightMeasure { get; set; }
            public string MWDH_WeightOnBitMeasure { get; set; }
            public string MWDH_HeadSpeedMeasure { get; set; }
            public string MWDH_HeadTorqueMeasure { get; set; }
            public string MWDH_HammerImpactMeasure { get; set; }
            public string MWDH_ElapsedTimeMeasure { get; set; }
            public string MWDH_PenetrationrateMeasure { get; set; }
        }

    Thursday, November 30, 2017 10:54 PM
  • Friday, December 1, 2017 3:16 AM
  • I managed to solve this by creating a new entity and making my datatable enumerable.

    I can then iterate through the datatable and map to the Entity.

    Thanks for your help.

    Friday, December 1, 2017 3:19 AM