ObjectContext.AddObject(String, Object) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
개체 컨텍스트에 개체를 추가합니다.
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
엔터티 컨테이너 이름으로 정규화될 수 있는 엔터티 집합 이름을 나타냅니다.
예외
예제
다음은 새 제품을 추가하고 변경 내용을 데이터베이스에 저장하는 예제입니다.
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 상태에 추가 ObjectStateManagerDetached됩니다.
개체 컨텍스트에서 다른 개체와 관련된 새 개체를 만들 때 다음 메서드 중 하나를 사용하여 개체를 추가합니다.
메서드를 AddEntityCollection<TEntity> 호출하고 관련 개체를 지정합니다. 일대다 또는 다대다 관계에 대해 이 작업을 수행합니다.
Value 관련 개체의 EntityReference<TEntity> 속성을 설정합니다. 일대일 또는 다대일 관계에 대해 이 작업을 수행합니다.
개체가 분리된 상태 EntityKey인 경우 .
형식에 entitySetName 대한 규칙은 다음과 같습니다.
속성이 DefaultContainerName
nullentitySetName면 엔터티 컨테이너 이름<에서>와 같이 정규화되어야 합니다.<엔터티 집합 이름>입니다.그렇지 않은
nullentitySetName경우 DefaultContainerName엔터티 컨테이너 이름> 중 하나<일 수 있습니다.<엔터티 집합 이름> 또는 <엔터티 집합 이름>입니다.
object
EntityKey 값이 있고 entitySetName 값 EntitySet 이 있는 경우 엔터티 키는 엔터티 컨테이너 이름 및 엔터티 컨테이너 이름을 기반으로 entitySetName 찾은 키와 일치 EntitySet 해야 합니다.