In EF 4.1+, is there a difference between these 2 lines of code?dbContext.SomeEntitySet.Add(entityInstance);dbContext.Entry(entityInstance).State = EntityState.Added;Or do they do the same thing? I'm wondering if one might affect child collections / navigation properties differently than the other. 解决方案 When you use dbContext.SomeEntitySet.Add(entityInstance); the status for this and all its related entities/collections is set to added, while dbContext.Entry(entityInstance).State = EntityState.Added; adds also all the related entities/collections to the context but leaves them as unmodified.So if the entity that you are trying to create has a related entity (and it's value its not null), when you use Add it will create a new object for that child entity, while with the other way it won't. 这篇关于IDbSet.Add和DbEntityEntry.State = EntityState.Added有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 03:17