DbContext ChangeTracker: Id of Added Entity for Auditing

折月煮酒 提交于 2019-12-04 07:30:57

I'd store the references to the entities in a List<T> (or an array, or other such structure) and then make the call to the underlying implementation of SaveChanges.

When the call is complete, because your entities are reference types (best practices indicate they should be as they are mutable), the primary keys on those items in the list should be populated, at which point you can pass the list of changed entities to your audit layer.

Also, it appears that you are trying to add an entity that one Entity type and add it to another Entity type collection (whatever is exposed by ChangeLogs); I'd recommend against this. If you are entering items in another entity set, do not reuse those entities, but copy the instances to the proper entity type.

Use GUIDs for primary key so your id's can be generated without hitting the database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!