LINQ & Entity
dataContext 동시에 insert/ update /delete 진행하기
스티커
2010. 4. 6. 16:35
여러가지 방법중에 하나이므로 최선 방법 이라고는 할 수 없습니다.!~
코드만 보면 대충 알수있습니다.
public partial class Address { public bool IsNew { get { return this.Timestamp == null && !this.IsDeleted; } } public bool IsDeleted { get; set; } }
public static void SaveContact(Contact contact) { using (PimDataContext dataContext = CreateDataContext()) { if (contact.ContactID == 0) { dataContext.Contacts.InsertOnSubmit(contact); } else { dataContext.Contacts.Attach(contact, true); dataContext.Addresses.AttachAll(contact.Addresses.Where(a => !a.IsNew), true); dataContext.Addresses.InsertAllOnSubmit(contact.Addresses.Where(a => a.IsNew)); } dataContext.SubmitChanges(); } }