Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Entity Framework mapping issue

Entity Framework mapping issue

Scheduled Pinned Locked Moved .NET (Core and Framework)
javatutorialcsharpphpcss
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alex barylski
    wrote on last edited by
    #1

    Greetings all - I haven't posted here in ages :) I am entirely new to .Net having been working in it for a week at most so please go easy and be detailed as possible :) I have the following PONO:

    Public Class WorkOrderEntity

    Private intTrackingNumber As Integer
    Private intDateReceived As Integer
    Private strManufacturer As String
    
    Public Property TrackingNumber() As Integer
        Get
            Return intTrackingNumber
        End Get
    
        Set(value As Integer)
            intTrackingNumber = value
        End Set
    End Property
    
    Public Property DateReceived() As String
        Get
            ' TODO: Convert timestamp to formatted date
            Return intDateReceived
        End Get
    
        Set(value As String)
            ' TODO: Convert formatted date to timestamp
            intDateReceived = value
        End Set
    End Property
    

    End Class

    The issue I am faced with is how to store date/time as a timestamp (integer) but provide public properties which format/convert accordingly. I suppose I could provide a an additional getter()/setter() but ideally I wonder if EF has a way of circumventing this "convention"? Additionally - I am also curious as to whether it's possible to map properties to columns which are not labeled correctly? Basically if I were working in a existing database (EF automatically builds my PONO with properties named after table fields) I wish to name the fields something more meaningful; some fields for example might be awkward abbreviations but in the object model I want something more English friendly? I seem to recall being able to do this with Hibernate in java (actually it's PHP port) but never the less does EF support such a feature? Seems obvious that this would be useful in day to day development??? Any ideas? Regards, Alex

    Richard DeemingR 1 Reply Last reply
    0
    • A alex barylski

      Greetings all - I haven't posted here in ages :) I am entirely new to .Net having been working in it for a week at most so please go easy and be detailed as possible :) I have the following PONO:

      Public Class WorkOrderEntity

      Private intTrackingNumber As Integer
      Private intDateReceived As Integer
      Private strManufacturer As String
      
      Public Property TrackingNumber() As Integer
          Get
              Return intTrackingNumber
          End Get
      
          Set(value As Integer)
              intTrackingNumber = value
          End Set
      End Property
      
      Public Property DateReceived() As String
          Get
              ' TODO: Convert timestamp to formatted date
              Return intDateReceived
          End Get
      
          Set(value As String)
              ' TODO: Convert formatted date to timestamp
              intDateReceived = value
          End Set
      End Property
      

      End Class

      The issue I am faced with is how to store date/time as a timestamp (integer) but provide public properties which format/convert accordingly. I suppose I could provide a an additional getter()/setter() but ideally I wonder if EF has a way of circumventing this "convention"? Additionally - I am also curious as to whether it's possible to map properties to columns which are not labeled correctly? Basically if I were working in a existing database (EF automatically builds my PONO with properties named after table fields) I wish to name the fields something more meaningful; some fields for example might be awkward abbreviations but in the object model I want something more English friendly? I seem to recall being able to do this with Hibernate in java (actually it's PHP port) but never the less does EF support such a feature? Seems obvious that this would be useful in day to day development??? Any ideas? Regards, Alex

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      When you say "timestamp", what do you mean? If it's the SQL timestamp type[^], this has nothing to do with dates:

      MSDN:[^]

      a data type that exposes automatically generated, unique binary numbers within a database. The timestamp data type is just an incrementing number and does not preserve a date or a time.

      You can map a property to a column with a different name using either the Column attribute[^] or the fluent API[^].


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      A 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        When you say "timestamp", what do you mean? If it's the SQL timestamp type[^], this has nothing to do with dates:

        MSDN:[^]

        a data type that exposes automatically generated, unique binary numbers within a database. The timestamp data type is just an incrementing number and does not preserve a date or a time.

        You can map a property to a column with a different name using either the Column attribute[^] or the fluent API[^].


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        A Offline
        A Offline
        alex barylski
        wrote on last edited by
        #3

        I just found some articles that explain how to change the mapping for tables so I imagine columns are just around the corner. :) When I say timestamp I mean a unix timestamp which can easily be converted to date AND/OR time - not sure what MSDN is getting at?!? In the database I wish to store the date "10/10/2013" as 1381381200 but convert to a human friendly date within the PONO. My entity objects are high level abstractions but I still prefer to store the time in a numeric format - how does EF solve/address this type mis-match? This is/was the impetus behind OR/M frameworks ain't it? :) Regards, Alex

        Richard DeemingR D 2 Replies Last reply
        0
        • A alex barylski

          I just found some articles that explain how to change the mapping for tables so I imagine columns are just around the corner. :) When I say timestamp I mean a unix timestamp which can easily be converted to date AND/OR time - not sure what MSDN is getting at?!? In the database I wish to store the date "10/10/2013" as 1381381200 but convert to a human friendly date within the PONO. My entity objects are high level abstractions but I still prefer to store the time in a numeric format - how does EF solve/address this type mis-match? This is/was the impetus behind OR/M frameworks ain't it? :) Regards, Alex

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Ah, OK - "timestamp" in SQL is something totally different. I don't think there's any built-in way to perform this sort of conversion in the mapping layer. The simplest approach is probably to add a calculated property for the date, decorated with the NotMapped attribute:

          Public Class WorkOrderEntity
          Private Shared ReadOnly Epoch As New DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)

          <Column("DateReceived")> _
          Public Property DateReceivedTimestamp() As Integer

          <NotMapped> _
          Public Property DateReceived() As DateTimeOffset
          Get
          Return Epoch.AddSeconds(DateReceivedTimestamp)
          End Get
          Set
          DateReceivedTimestamp = CInt(Math.Floor((value - Epoch).TotalSeconds))
          End Set
          End Property
          End Class

          I'd be inclined to use DateTimeOffset for the property rather than a string; formatting the date should be a UI concern.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • A alex barylski

            I just found some articles that explain how to change the mapping for tables so I imagine columns are just around the corner. :) When I say timestamp I mean a unix timestamp which can easily be converted to date AND/OR time - not sure what MSDN is getting at?!? In the database I wish to store the date "10/10/2013" as 1381381200 but convert to a human friendly date within the PONO. My entity objects are high level abstractions but I still prefer to store the time in a numeric format - how does EF solve/address this type mis-match? This is/was the impetus behind OR/M frameworks ain't it? :) Regards, Alex

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            EF will see a .NET type of DateTime in your POCO and use the native DateTime datatype specified by the underlying data provider. It will not convert a DateTime to a "serial number" as you want because the serial number can be based on any arbitrary epoch. For example, UNIX uses 1/1/1970 00:00:00AM whereas .NET uses 1/1/0001 00:00:00AM. So, to each system, the datetime serial number will mean something different. Why even do this?? Why not just let the underlying database handle the storage and return the correct datetime without your code having to worry about conversion?

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups