none
EF Entity "Optional" Mapping of Property-to-Underlying Table Field RRS feed

  • Question

  • Hello,

    Wanted to see if a certain scenario I had in mind was possible to pull off.

    I have an entity (POCO) with a specific "mapped" property to a corresponding table in the underlying SQL Server database.  I'd like this property to be "optionally" mapped basically.  That is, in the absence of the corresponding field in the underlying table, I don't want the dbContext to throw an exception of any kind.  And I don't mean applying the [NotMapped] attribute to the field.  I still want the correct mapping to occur IF the field is present.  But I don't want an exception raised when the table field is absent from the schema ("optional" mapping).

    If there a way of doing this?

    Thanks very much,

    Mike

    Tuesday, September 3, 2019 4:16 PM

Answers

  • Hi,

    I am afraid dbContext does not have such a feature. You can only judge for yourself whether the field should be mapped, and then manually map to the database using code.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 4, 2019 8:41 AM
    Moderator

All replies

  • Hi,

    I am afraid dbContext does not have such a feature. You can only judge for yourself whether the field should be mapped, and then manually map to the database using code.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 4, 2019 8:41 AM
    Moderator
  • Thanks for the response Kyle. I wondered what you meant by "manually map to the database using code"?

    Thanks again.

    Wednesday, September 4, 2019 9:34 PM