locked
Converting a property value in EF RRS feed

  • Question

  • I'm creating an EF application using code first and POCO classes. One of these POCO classes should have a certain property that is of a certain class, for example:

    public MyClass MyProperty { get; set; }

    The MyClass object is not another database table however, so it is not a POCO class. This property should be saved in a serialized form in the database, such as byte array or string. Can I somehow make this mapping work? I can think of 2 theoretical solutions, but I have no idea if they are possible in EF.

    1) Create some kind of value converter, converting from byte[] to MyClass and the other way around whenever the POCO class is saved/loaded.

    2) Create a 2nd property that contains the byte[] and trigger synchronisation between the 2 properties whenever the object is loaded or will be saved.

    Does anybody know if these options are possible in EF and how?

    Regards, Stefan

    Friday, April 19, 2013 9:04 AM

Answers

  • Hi Stefan,

    Base on my experience, we cannot use Data Annotations or Fluent API to map complex type to database.

    For your second approach, you may refer to this thread.

    http://forums.asp.net/t/1753681.aspx/1

    Create another property and use setter and getter to serialize / deserialize and synchronize the properties.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stefan_A Tuesday, April 23, 2013 7:12 AM
    Tuesday, April 23, 2013 6:40 AM

All replies

  • Hi Stefan,

    Base on my experience, we cannot use Data Annotations or Fluent API to map complex type to database.

    For your second approach, you may refer to this thread.

    http://forums.asp.net/t/1753681.aspx/1

    Create another property and use setter and getter to serialize / deserialize and synchronize the properties.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stefan_A Tuesday, April 23, 2013 7:12 AM
    Tuesday, April 23, 2013 6:40 AM
  • Ok, that's clear, thanks!
    Tuesday, April 23, 2013 7:12 AM