共用方式為


DbContext.AddAsync Method

Definition

Overloads

AddAsync(Object, CancellationToken)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

AddAsync<TEntity>(TEntity, CancellationToken)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

AddAsync(Object, CancellationToken)

Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

public virtual System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry> AddAsync (object entity, System.Threading.CancellationToken cancellationToken = default);
public virtual System.Threading.Tasks.ValueTask<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry> AddAsync (object entity, System.Threading.CancellationToken cancellationToken = default);
abstract member AddAsync : obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry>
override this.AddAsync : obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry>
abstract member AddAsync : obj * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry>
override this.AddAsync : obj * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry>
Public Overridable Function AddAsync (entity As Object, Optional cancellationToken As CancellationToken = Nothing) As Task(Of EntityEntry)
Public Overridable Function AddAsync (entity As Object, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of EntityEntry)

Parameters

entity
Object

The entity to add.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

A task that represents the asynchronous Add operation. The task result contains the EntityEntry for the entity. The entry provides access to change tracking information and operations for the entity.

Exceptions

Remarks

Use State to set the state of only a single entity.

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See EF Core change tracking for more information and examples.

Applies to

AddAsync<TEntity>(TEntity, CancellationToken)

Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs
Source:
DbContext.cs

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

public virtual System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity>> AddAsync<TEntity> (TEntity entity, System.Threading.CancellationToken cancellationToken = default) where TEntity : class;
public virtual System.Threading.Tasks.ValueTask<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity>> AddAsync<TEntity> (TEntity entity, System.Threading.CancellationToken cancellationToken = default) where TEntity : class;
abstract member AddAsync : 'Entity * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<'Entity>> (requires 'Entity : null)
override this.AddAsync : 'Entity * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<'Entity>> (requires 'Entity : null)
abstract member AddAsync : 'Entity * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<'Entity>> (requires 'Entity : null)
override this.AddAsync : 'Entity * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<'Entity>> (requires 'Entity : null)
Public Overridable Function AddAsync(Of TEntity As Class) (entity As TEntity, Optional cancellationToken As CancellationToken = Nothing) As Task(Of EntityEntry(Of TEntity))
Public Overridable Function AddAsync(Of TEntity As Class) (entity As TEntity, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of EntityEntry(Of TEntity))

Type Parameters

TEntity

The type of the entity.

Parameters

entity
TEntity

The entity to add.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task<EntityEntry<TEntity>>

A task that represents the asynchronous Add operation. The task result contains the EntityEntry<TEntity> for the entity. The entry provides access to change tracking information and operations for the entity.

Exceptions

Remarks

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See EF Core change tracking for more information and examples.

Applies to