ObjectContext.AddObject(String, Object) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Adds an object to the object context.
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)
Parameters
- entitySetName
- String
Represents the entity set name, which may optionally be qualified by the entity container name.
Exceptions
Examples
This example adds a new product and saves the changes to the database.
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);
}
}
Remarks
Call AddObject on the ObjectContext to add the object to the object context. Do this when the object is a new object that does not yet exist in the data source. For more information, see Attaching and Detaching Objects.
Objects are added to the ObjectStateManager in the Detached, Deleted or Added state.
When you create a new object that is related to another object in the object context, add the object by using one of the following methods:
Call the Add method on the EntityCollection<TEntity> and specify the related object. Do this for a one-to-many or many-to-many relationship.
Set the Value property of the EntityReference<TEntity> to the related object. Do this for a one-to-one or many-to-one relationship.
For more information, see Creating, Adding, Modifying, and Deleting Objects.
If the object is in a detached state it must not have an EntityKey.
The rules for the entitySetName
format are as follows:
If the DefaultContainerName property is
null
, then theentitySetName
has to be fully qualified as in <Entity Container Name>.<Entity Set Name>.If DefaultContainerName is not
null
, then theentitySetName
can be either <Entity Container Name>.<Entity Set Name> or <Entity Set Name>.
If the object
has an EntityKey and entitySetName
has a value, then the EntitySet of the entity key must match the EntitySet that was found based on the entitySetName
and entity container name.