ObjectContext.SaveChanges 메서드

정의

데이터 원본에 대한 모든 업데이트를 유지합니다.

오버로드

Name Description
SaveChanges()

데이터 원본에 대한 모든 업데이트를 유지하며 개체 컨텍스트에서 변경 내용 추적을 다시 설정합니다.

SaveChanges(Boolean)
사용되지 않음.

데이터 원본에 대한 모든 업데이트를 유지하며 필요에 따라 개체 컨텍스트에서 변경 내용 추적을 다시 설정합니다.

SaveChanges(SaveOptions)

지정된 SaveOptions데이터 원본에 대한 모든 업데이트를 유지합니다.

SaveChanges()

데이터 원본에 대한 모든 업데이트를 유지하며 개체 컨텍스트에서 변경 내용 추적을 다시 설정합니다.

public:
 int SaveChanges();
public int SaveChanges();
member this.SaveChanges : unit -> int
Public Function SaveChanges () As Integer

반품

또는 호출된 상태 SaveChanges() 의 개체 AddedModifiedDeleted 수입니다.

예외

데이터 원본에서 낙관적 동시성 위반이 발생했습니다.

예제

이 예제에서는 동시성 충돌을 일으킬 수 있는 변경 내용을 저장하려고 시도합니다. 그런 다음, 변경 내용을 다시 저장하기 전에 개체 컨텍스트를 새로 고쳐 동시성 충돌을 해결하는 방법을 보여 줍니다.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Perform an operation with a high-level of concurrency.
        // Change the status of all orders without an approval code.
        ObjectQuery<SalesOrderHeader> orders =
            context.SalesOrderHeaders.Where(
            "it.CreditCardApprovalCode IS NULL").Top("100");

        foreach (SalesOrderHeader order in orders)
        {
            // Reset the order status to 4 = Rejected.
            order.Status = 4;
        }
        try
        {
            // Try to save changes, which may cause a conflict.
            int num = context.SaveChanges();
            Console.WriteLine("No conflicts. " +
                num.ToString() + " updates saved.");
        }
        catch (OptimisticConcurrencyException)
        {
            // Resolve the concurrency conflict by refreshing the
            // object context before re-saving changes.
            context.Refresh(RefreshMode.ClientWins, orders);

            // Save changes.
            context.SaveChanges();
            Console.WriteLine("OptimisticConcurrencyException "
            + "handled and changes saved");
        }

        foreach (SalesOrderHeader order in orders)
        {
            Console.WriteLine("Order ID: " + order.SalesOrderID.ToString()
                + " Order status: " + order.Status.ToString());
        }
    }
    catch (UpdateException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

설명

클라이언트의 개체가 데이터 원본 쪽 논리에 의해 업데이트되었는지 확인하려면 호출한 후 값을 사용하여 메서드를 StoreWins 호출 RefreshSaveChanges할 수 있습니다.

SaveChanges 는 트랜잭션 내에서 작동합니다. SaveChanges 는 해당 트랜잭션을 롤백하고 더티 ObjectStateEntry 개체를 유지할 수 없는 경우 예외를 throw합니다.

낙관적 동시성 위반이 발생하면 throw OptimisticConcurrencyException 됩니다. 낙관적 동시성 위반을 catch하고, 메서드를 값 ClientWins 으로 StoreWins 호출 Refresh 한 다음, 다시 호출 SaveChanges 하여 해결할 수 있습니다.

추가 정보

적용 대상

SaveChanges(Boolean)

주의

Use SaveChanges(SaveOptions options) instead.

데이터 원본에 대한 모든 업데이트를 유지하며 필요에 따라 개체 컨텍스트에서 변경 내용 추적을 다시 설정합니다.

public:
 int SaveChanges(bool acceptChangesDuringSave);
public int SaveChanges(bool acceptChangesDuringSave);
[System.ComponentModel.Browsable(false)]
[System.Obsolete("Use SaveChanges(SaveOptions options) instead.")]
public int SaveChanges(bool acceptChangesDuringSave);
member this.SaveChanges : bool -> int
[<System.ComponentModel.Browsable(false)>]
[<System.Obsolete("Use SaveChanges(SaveOptions options) instead.")>]
member this.SaveChanges : bool -> int
Public Function SaveChanges (acceptChangesDuringSave As Boolean) As Integer

매개 변수

acceptChangesDuringSave
Boolean

이 매개 변수는 클라이언트 쪽 트랜잭션 지원에 필요합니다. 이 경우 true모든 개체에 대한 변경 내용 추적이 완료된 후 SaveChanges(Boolean) 다시 설정됩니다. 이면 false메서드를 AcceptAllChanges()SaveChanges(Boolean)호출해야 합니다.

반품

또는 호출된 상태 SaveChanges() 의 개체 AddedModifiedDeleted 수입니다.

특성

예외

낙관적 동시성 위반이 발생했습니다.

설명

대신 메서드를 SaveChanges(SaveOptions) 호출합니다.

적용 대상

SaveChanges(SaveOptions)

지정된 SaveOptions데이터 원본에 대한 모든 업데이트를 유지합니다.

public:
 virtual int SaveChanges(System::Data::Objects::SaveOptions options);
public virtual int SaveChanges(System.Data.Objects.SaveOptions options);
abstract member SaveChanges : System.Data.Objects.SaveOptions -> int
override this.SaveChanges : System.Data.Objects.SaveOptions -> int
Public Overridable Function SaveChanges (options As SaveOptions) As Integer

매개 변수

options
SaveOptions

SaveOptions 작업의 동작을 결정하는 값입니다.

반품

또는 호출된 상태 SaveChanges() 의 개체 AddedModifiedDeleted 수입니다.

예외

낙관적 동시성 위반이 발생했습니다.

설명

이 특정 오버로드를 사용하여 데이터 원본에 SaveChanges 대한 변경 내용을 저장하기 전에 호출되는지 또는 AcceptAllChanges 데이터 원본에 변경 내용을 저장한 후에 호출되는지 확인 DetectChanges 합니다.

이 열거형에는 FlagsAttribute 멤버 값의 비트 조합을 허용하는 열거형이 있습니다.

클라이언트의 개체가 데이터 원본 쪽 논리에 의해 업데이트되었는지 확인하려면 호출한 후 값을 사용하여 메서드를 StoreWins 호출 RefreshSaveChanges할 수 있습니다. 메서드는 SaveChanges 트랜잭션에서 작동합니다. SaveChanges 는 해당 트랜잭션을 롤백하고 더티 ObjectStateEntry 개체 중 하나를 유지할 수 없는 경우 예외를 throw합니다.

낙관적 동시성 위반이 발생하면 throw OptimisticConcurrencyException 됩니다. 낙관적 동시성 위반을 catch하고, 메서드를 값 ClientWins 으로 StoreWins 호출 Refresh 한 다음, 메서드를 SaveChanges 다시 호출하여 해결할 수 있습니다.

적용 대상