public static Task<int> InsertEntityAsync<TEntity>(
this DbContext dataContext,
TEntity entity,
CancellationToken cancellationToken = default
)
<ExtensionAttribute>
Public Shared Function InsertEntityAsync(Of TEntity) (
dataContext As DbContext,
entity As TEntity,
Optional cancellationToken As CancellationToken = Nothing
) As Task(Of Integer)public:
[ExtensionAttribute]
generic<typename TEntity>
static Task<int>^ InsertEntityAsync(
DbContext^ dataContext,
TEntity entity,
CancellationToken cancellationToken = CancellationToken()
)[<ExtensionAttribute>]
static member InsertEntityAsync :
dataContext : DbContext *
entity : 'TEntity *
?cancellationToken : CancellationToken
(* Defaults:
let _cancellationToken = defaultArg cancellationToken new CancellationToken()
*)
-> Task<int> The stored procedure name is determined by looking for the InsertEntityStoredProcedureAttribute on the entity type. The stored procedure must have parameters for each of the entity properties except those marked with the IgnoreAttribute for inserts. It should not return a value or a result set. Parameters related to the primary key (single column, integer only) or marked with the TimestampAttribute are defined as input/out parameters. All other parameters are input only.
If the connection is not in an open state, it is opened temporarily while updating the entity. If change tracking is enabled on the data context and the entity its state will be set to unchanged.
using var dataContext = new MyDbContext();
if(watchListItem.WatchID == 0)
await dataContext.InsertEntityAsync(watchListItem);
else
await dataContext.UpdateEntityAsync(watchListItem);