ObjectContext.AddObject(String, Object) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Aggiunge un oggetto al contesto dell'oggetto.
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)
Parametri
- entitySetName
- String
Rappresenta il nome del set di entità, che può essere facoltativamente qualificato dal nome del contenitore di entità.
Eccezioni
Esempio
In questo esempio viene aggiunto un nuovo prodotto e le modifiche vengono salvate nel 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);
}
}
Commenti
Chiamare AddObject su ObjectContext per aggiungere l'oggetto al contesto dell'oggetto. Eseguire questa operazione quando l'oggetto è un nuovo oggetto che non esiste ancora nell'origine dati.
Gli oggetti vengono aggiunti all'oggetto ObjectStateManagerDetachednello stato o DeletedAdded .
Quando si crea un nuovo oggetto correlato a un altro oggetto nel contesto dell'oggetto, aggiungere l'oggetto utilizzando uno dei metodi seguenti:
Chiamare il Add metodo su EntityCollection<TEntity> e specificare l'oggetto correlato. Eseguire questa operazione per una relazione uno-a-molti o molti-a-molti.
Impostare la Value proprietà dell'oggetto EntityReference<TEntity> sull'oggetto correlato. Eseguire questa operazione per una relazione uno-a-uno o molti-a-uno.
Se l'oggetto si trova in uno stato scollegato, non deve avere un oggetto EntityKey.
Le regole per il entitySetName formato sono le seguenti:
Se la proprietà è DefaultContainerName, l'oggetto
nullentitySetNamedeve essere completo come in <Entity Container Name>.<Nome set> di entità.Se DefaultContainerName non
nullè , èentitySetNamepossibile usare il nome del <contenitore> di entità.<Nome set> di entità o <nome> set di entità.
Se ha object e EntityKeyentitySetName ha un valore, l'oggetto della chiave di entità deve corrispondere a EntitySet quello EntitySet trovato in base al nome del entitySetName contenitore di entità e .