Share via


ObjectContext.AddObject(String, Object) 메서드

정의

개체를 개체 컨텍스트에 추가합니다.

public:
 void AddObject(System::String ^ entitySetName, System::Object ^ entity);
public void AddObject (string entitySetName, object entity);
member this.AddObject : string * obj -> unit
Public Sub AddObject (entitySetName As String, entity As Object)

매개 변수

entitySetName
String

엔터티 컨테이너 이름으로 정규화될 수도 있는 엔터티 집합 이름을 나타냅니다.

entity
Object

추가할 Object입니다.

예외

entity 매개 변수가 null인 경우

또는

entitySetName을 정규화할 수 없는 경우

예제

다음은 새 제품을 추가하고 데이터베이스에 변경 내용을 저장하는 예제입니다.

Product newProduct;

// Define values for the new product.
string dateTimeString = "1998-06-01 00:00:00.000";
string productName = "Flat Washer 10";
string productNumber = "FW-5600";
Int16 safetyStockLevel = 1000;
Int16 reorderPoint = 750;

// Convert the date time string into a DateTime instance.
DateTime sellStartDate;
if (!DateTime.TryParse(dateTimeString, out sellStartDate))
{
    throw new ArgumentException(string.Format("The string '{0}'cannot "
        + "be converted to DateTime.", dateTimeString));
}

// Create a new Product.
newProduct = Product.CreateProduct(0,
    productName, productNumber, false, false, safetyStockLevel, reorderPoint,
    0, 0, 0, DateTime.Today, Guid.NewGuid(), DateTime.Today);

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Add the new object to the context.
        context.Products.AddObject(newProduct);

        // Persist the new produc to the data source.
        context.SaveChanges();

        // Return the identity of the new product.
        return newProduct.ProductID;
    }
    catch (UpdateException ex)
    {
        throw new InvalidOperationException(string.Format(
            "The object could not be added. Make sure that a "
            + "product with a product number '{0}' does not aleady exist.\n",
            newProduct.ProductNumber), ex);
    }
}

설명

ObjectContext 호출 AddObject 하여 개체 컨텍스트에 개체를 추가합니다. 개체가 데이터 원본에 아직 없는 새 개체인 경우 이 작업을 수행합니다. 자세한 내용은 개체 연결 및 분리를 참조하세요.

개체는 또는 DeletedAdded 상태의 에 Detached추가 ObjectStateManager 됩니다.

개체 컨텍스트에서 다른 개체와 관련된 새 개체를 만들 때 다음 메서드 중 하나를 사용하여 개체를 추가합니다.

  • 에서 EntityCollection<TEntity> 메서드를 Add 호출하고 관련 개체를 지정합니다. 일대다 또는 다 대 다 관계에 대해 이 작업을 수행합니다.

  • Value 속성을 EntityReference<TEntity> 관련 개체로 설정합니다. 일대일 또는 다대일 관계에 대해 이 작업을 수행합니다.

자세한 내용은 만들기, 추가, 수정, 및 개체 삭제합니다.

개체가 분리된 상태이면 가 없어야 EntityKey합니다.

형식에 entitySetName 대한 규칙은 다음과 같습니다.

  • 속성nullDefaultContainerNameentitySetName 경우 는 엔터티 컨테이너 이름>에서<와 같이 정규화되어야 합니다.<엔터티 집합 이름>입니다.

  • 가 이 아닌 null경우 DefaultContainerNameentitySetName엔터티 컨테이너 이름> 중 하나<일 수 있습니다.<엔터티 집합 이름> 또는 <엔터티 집합 이름>입니다.

object 있고 값이 있는 경우 엔터티 키의 는 및 엔터티 컨테이너 이름을 기반으로 entitySetName 찾은 와 일치 EntitySet 해야 합니다.EntitySetentitySetNameEntityKey

적용 대상

추가 정보