When adding an object using the object framework, we cannot directly call the addobject () method when adding an object containing the referenced object, unless the entity itself does not reference any other entity! However, in a project, many entities reference one or more other entities. To add such entities to the object framework, you must first query the entities of all referenced objects, add the object to the entitycollection of each referenced object. The following uses userinfo and userstate as an example:
Userinfo to use userstate, userstate does not reference any object;
1. If we add userstate, we can directly call objectcontext. addtotablename (object) to add an object or call the addobject () method of objectcontext to add it.
2. if we add userinfo, we cannot directly add it. Instead, we must first query the userstate corresponding to userinfo and then call system in userstate. data. objects. dataclasses. the add () method of entitycollection <userinfo> adds userinfo, and then calls the savechanges () method of objectcontext to complete the operation.