Migration:DbSet.AddOrUpdate trouble

2019-09-06 02:59发布

问题:

When I use the method in Migration class like this:

DbSet<T>.AddOrUpdate( new Instance(){});

the method just generate insert T-SQL, and I receive one exception, because The row has exists in the table.

the slibing method in Migration class like this:

DbSet<T>.AddOrUpdate( p=>p.name,new Instance(){}): 

I can't transform this in my fashion( the table has one join index(Column1,Column2)).

now my question is: how to use the Method

public static void AddOrUpdate<TEntity>(
    this IDbSet<TEntity> set,
    Expression<Func<TEntity, Object>> identifierExpression,
    params TEntity[] entities
)where TEntity : class

the parameter identifierExpression I must defined tow column of the table to determine whether an Add or Update operation should be performed

回答1:

using this statement will resolve in a problem, because it cannot index your columns.

DbSet<T>.AddOrUpdate( new Instance(){});

Should you use this instead:

context.People.AddOrUpdate(p => new { p.FirstName, p.LastName }, people);