Working with beforeSaveEntity and Navigation Properties

前端 未结 2 464
青春惊慌失措
青春惊慌失措 2020-12-21 16:13

My application allows the user to create products along with their UOM (Units of Measurement) and Barcodes During the creation process, API will check if there is no barcode

2条回答
  •  隐瞒了意图╮
    2020-12-21 16:23

    When the BeforeSaveEntity and BeforeSaveEntities methods are called, the entities have just been materialized from the JSON stream. They haven't been added to the EF context yet. Their data properties are all populated, but their navigation properties are not. So the relationships that you need are not yet available.

    If the related entities (Barcode and Unit and Product, in your case) are in the same change set, you will need to use BeforeSaveEntities (which gives you access to the full saveMap) and find the related entities in the saveMap manually (i.e. find them by type and by key).

    If the related entities you need are not in the saveMap, you will need to find them from the data store. In EF you would do that by creating a new EF Context, as Ward described in his answer.

提交回复
热议问题