Creating a non mapped property in an entity (entity framework)

前端 未结 4 2085
[愿得一人]
[愿得一人] 2020-12-09 07:32

I want to create a custom property on one of my entities mapped from the database, however this property is not mapped to the database, I created the property using partial

4条回答
  •  [愿得一人]
    2020-12-09 08:05

    I'm seriously late to the conversation, but you also want to mark the partial as serializable and the property as serializable - if you ever plan to JSON or serialize the objects:

    [Serializable()] 
    public partial class MyClass {
        private System.Nullable _Age;
        [global::System.Runtime.Serialization.DataMemberAttribute(Order = 4)] 
        public System.Nullable Age {
                ...
        }
    }
    

    Both the [Serializable()] and the [global:] directives are needed. If you excluded the [global:], any time you serialized it, it'd be ignored and not included in the serialization.

提交回复
热议问题