Table<TEntity>.AttachAll Metodo

Definizione

Associa tutte le entità di una raccolta a DataContext in uno stato modificato o non modificato.

Overload

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

Associa tutte le entità di una raccolta a DataContext in uno stato modificato o non modificato.

AttachAll<TSubEntity>(IEnumerable<TSubEntity>, Boolean)

Associa tutte le entità di una raccolta a DataContext in uno stato modificato o non modificato.

Commenti

Se il collegamento viene modificato, l'entità deve dichiarare un membro della versione o non deve partecipare al controllo dei conflitti di aggiornamento.

Quando viene associata una nuova entità, vengono inizializzati i caricatori posticipati per tutte le raccolte figlio (ad esempio raccolte EntitySet di entità da tabelle associate). Quando SubmitChanges viene chiamato, i membri delle raccolte figlio vengono inseriti in uno Unmodified stato. Per aggiornare i membri di una raccolta figlio, è necessario chiamare Attach in modo esplicito e specificare tale entità.

Per altre informazioni, vedere Operazioni di recupero dati e CUD in applicazioni a più livelli (LINQ to SQL).For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

Associa tutte le entità di una raccolta a DataContext in uno stato modificato o non modificato.

public:
generic <typename TSubEntity>
 where TSubEntity : TEntity void AttachAll(System::Collections::Generic::IEnumerable<TSubEntity> ^ entities);
public void AttachAll<TSubEntity> (System.Collections.Generic.IEnumerable<TSubEntity> entities) where TSubEntity : TEntity;
member this.AttachAll : seq<#'Entity> -> unit
Public Sub AttachAll(Of TSubEntity As TEntity) (entities As IEnumerable(Of TSubEntity))

Parametri di tipo

TSubEntity

Tipo di entità da associare.

Parametri

entities
IEnumerable<TSubEntity>

Insieme di entità.

Commenti

Questo metodo associa tutte le entità di una raccolta a un nuovo DataContextoggetto . Quando viene associata una nuova entità, vengono inizializzati i caricatori posticipati per tutte le raccolte figlio (ad esempio raccolte EntitySet di entità da tabelle associate). Quando SubmitChanges viene chiamato, i membri delle raccolte figlio vengono inseriti in uno Unmodified stato. Per aggiornare i membri di una raccolta figlio, è necessario chiamare Attach in modo esplicito e specificare tale entità.

Per altre informazioni, vedere Operazioni di recupero dati e CUD in applicazioni a più livelli (LINQ to SQL).For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

Si applica a

AttachAll<TSubEntity>(IEnumerable<TSubEntity>, Boolean)

Associa tutte le entità di una raccolta a DataContext in uno stato modificato o non modificato.

public:
generic <typename TSubEntity>
 where TSubEntity : TEntity void AttachAll(System::Collections::Generic::IEnumerable<TSubEntity> ^ entities, bool asModified);
public void AttachAll<TSubEntity> (System.Collections.Generic.IEnumerable<TSubEntity> entities, bool asModified) where TSubEntity : TEntity;
member this.AttachAll : seq<#'Entity> * bool -> unit
Public Sub AttachAll(Of TSubEntity As TEntity) (entities As IEnumerable(Of TSubEntity), asModified As Boolean)

Parametri di tipo

TSubEntity

Tipo di entità da associare.

Parametri

entities
IEnumerable<TSubEntity>

Insieme di entità.

asModified
Boolean

true se l’oggetto è associato a un timestamp o un elemento RowVersion; false se i valori originali vengono utilizzati per il controllo della concorrenza ottimistica.

Esempio

Nell'esempio seguente viene illustrato come aggiornare un Order oggetto in un'istanza diversa DataContext . Nell'esempio si presuppone che sia stata stabilita una connessione a un database e che sia stato creato un file di LINQ to SQL (in questo caso, il database di esempio Northwind).

using (Northwnd db = new Northwnd(@"c:\northwnd.mdf"))
{
    // Get original Customer from deserialization.
    var q1 = db.Orders.First();
    string serializedQ = SerializeHelper.Serialize(q1);
    var q2 = SerializeHelper.Deserialize(serializedQ, q1);

    // Track this object for an update (not insert).
    db.Orders.Attach(q2, false);

    // Replay the changes.
    q2.ShipRegion = "King";
    q2.ShipAddress = "1 Microsoft Way";

    // DataContext knows how to update the order.
    db.SubmitChanges();
}
Using db As New Northwnd("")
    ' Get original Customer from deserialization.
    Dim q1 = db.Orders.First()
    Dim serializedQ As String = SerializeHelper.Serialize(q1)
    Dim q2 = SerializeHelper.Deserialize(serializedQ, q1)

    ' Track this object for an update (not insert).
    db.Orders.Attach(q2, False)

    ' Replay the changes.
    q2.ShipRegion = "King"
    q2.ShipAddress = "1 Microsoft Way"

    ' DataContext knows how to update the order.
    db.SubmitChanges()
End Using

Nell'esempio seguente un oggetto entità da associare ha una relazione di chiave esterna con un altro oggetto e viene archiviato nella cache ma non collegato. Quando si chiama SubmitChanges, aggiunge ChangeProcessor un'operazione Insert per tutti gli oggetti chiave esterna. Si tratta di un effetto collaterale quando un'istanza di entità viene riutilizzata in un'istanza diversa DataContext . Per questo motivo, LINQ to SQL non supporta il riutilizzo degli oggetti.

Customer c = null;
using (Northwnd db = new Northwnd(""))
{
    /* Get both the customer c and the customer's order
    into the cache. */
    c = db.Customers.First();
    string sc = c.Orders.First().ShipCity;
}

using (Northwnd nw2 = new Northwnd(""))
{
    // Attach customers and update the address.
    nw2.Customers.Attach(c, false);
    c.Address = "new";
    nw2.Log = Console.Out;

    /* At SubmitChanges, you will see INSERT requests for all
    Customer c’s orders. */
    nw2.SubmitChanges();
}
Sub method7()
    Dim c As Customer = Nothing
    Using db = New Northwnd("...")
        ' Get both the customer c and the customer's order
        ' into the cache.
        c = db.Customers.First()
        Dim sc = c.Orders.First().ShipCity
    End Using

    Using nw2 = New Northwnd("...")
        ' Attach customers and update the address.
        nw2.Customers.Attach(c, False)
        c.Address = "new"
        nw2.Log = Console.Out

        ' At SubmitChanges, you will see INSERT requests for all
        ' c's orders.
        nw2.SubmitChanges()
    End Using

L'esempio seguente illustra uno scenario in cui il cliente A ha annullato tutti gli ordini e il cliente B ne ha acquisito la proprietà. È possibile allegare tutti gli ordini del cliente A contemporaneamente.

Customer CustA_File = new Customer();
Customer CustB_File = new Customer();
string xmlFileA = "";
string xmlFileB = "";

// Get the serialized objects.
Customer A = SerializeHelper.Deserialize<Customer>(xmlFileA, CustA_File);
Customer B = SerializeHelper.Deserialize<Customer>(xmlFileB, CustB_File);
List<Order> AOrders = A.Orders.ToList();

using (Northwnd db = new Northwnd(@"c:\northwnd.mdf"))

{
    //Attach all the orders belonging to Customer A all at once.
    db.Orders.AttachAll(AOrders, false);

    // Update the orders belonging to Customer A to show them
    // as owned by Customer B.
    foreach (Order o in AOrders)
    {
        o.CustomerID = B.CustomerID;
    }

    // DataContext can now apply the change of ownership to
    // the database.
    db.SubmitChanges();
}
Dim custA_File = New Customer()
Dim custB_File = New Customer()
Dim xmlFileA As String = ""
Dim xmlFileB As String = ""

' Get the serialized objects.
Dim A As Customer = SerializeHelper.Deserialize(Of Customer)(xmlFileA, custA_File)
Dim B As Customer = SerializeHelper.Deserialize(Of Customer)(xmlFileB, custB_File)

Dim AOrders As List(Of Order) = A.Orders.ToList()

Using db As New Northwnd("...")
    'Attach all the orders belonging to Customer A all at once.
    db.Orders.AttachAll(AOrders, False)

    ' Update the orders belonging to Customer A to show them
    ' as owned by Customer B
    For Each o In AOrders
        o.CustomerID = B.CustomerID
    Next

    ' DataContext can now apply the change of ownership to
    'the database
    db.SubmitChanges()
End Using

Commenti

Questo metodo associa tutte le entità di una raccolta a DataContext in uno stato modificato o non modificato . Se il collegamento viene modificato, l'entità deve dichiarare un membro della versione o non deve partecipare al controllo dei conflitti di aggiornamento. Se si associa come non modificato, si presuppone che l'entità rappresenti il valore originale. Dopo aver chiamato questo metodo, i campi dell'entità possono essere modificati con altre informazioni dal client prima SubmitChanges della chiamata. Per altre informazioni, vedere Operazioni di recupero dati e CUD in applicazioni a più livelli (LINQ to SQL).For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

Quando viene associata una nuova entità, vengono inizializzati i caricatori posticipati per tutte le raccolte figlio (ad esempio raccolte EntitySet di entità da tabelle associate). Quando SubmitChanges viene chiamato, i membri delle raccolte figlio vengono inseriti in uno Unmodified stato. Per aggiornare i membri di una raccolta figlio, è necessario chiamare Attach in modo esplicito e specificare tale entità.

Si applica a