UnMapped property on the Angular/Breeze SPA template

后端 未结 3 1121
逝去的感伤
逝去的感伤 2020-12-20 09:47

I am using the Angular/Breeze SPA template in visual studio 2012. I have added an unmapped property in the TodoList server model.

[NotMapped]
public string M         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 10:27

    Some background for the reader.

    It's important to differentiate

    • "unmapped" from the EF perspective
    • "not serialized" from the Json.Net perspective
    • "unmapped" from the Breeze perspective

    [NotMapped] is an EF attribute that tells EF "this property is not mapped to the database".

    [JsonIgnore] is a Json.Net attribute that tells JSON.Net "don't serialize this property when you send it to the client"

    Only metadata can tell Breeze which properties are "mapped" ... from its perspective.

    Breeze generally ignores incoming properties that aren'd defined in metadata. However, if a non-metadata property that is defined in the constructor, Breeze adds it to metadata and classifies it as "unmapped".

    For Breeze that means "serialize this property, notify the UI when it changes, but this is not part of the entity's data model and should not be change-tracked (i.e., changes to it do not affect the EntityState)."

    Let's put these ideas together to understand your question.

    You told EF not to map the MyUnmappedProperty property. You're probably using the EFContextProvider to generate the metadata so that property isn't in the metadata either.

    But Json.Net doesn't know about any of this. Therefore, whenever it serializes an instance of your TodoList, it sends the MyUnmappedProperty property ("testString") to the Breeze client.

    At first Breeze won't do anything with it. If the server sends this property with any value, Breeze will ignore it.

    But you added the MyUnmappedProperty property to the TodoList type constructor so now Breeze will recognize this property as "unmapped" and will materialize its value if it appears in the JSON query results.

    When you create a new instance of TodoList (with new or entityManager.CreateEntity(...)), then MyUnmappedProperty property is set to '' per the ctor.

    When you query for TodoList, the Json.NET sends {MyUnmappedProperty: "testString} in the JSON results. The Breeze client recognizes the MyUnmappedProperty, and accordingly sets that property on the materialized TodoList entity instance.

    That's what is supposed to happen.

    Can you demonstrate that a queried TodoList is ever materialized such that its MyUnmappedProperty is not "testString"?

    You've checked the network traffic and you see that {MyUnmappedProperty: "testString} is actually coming over the wire but is not materialized? And you say that it is materialized in a second query?.

    I need a repro of that!

提交回复
热议问题