Using Dapper I would like to implement a method that accepts IEnumberabletype objects User. Now Userit looks like this:
public class User
{
public int UserId { get; internal set; }
public DateTime DateCreated { get; internal set; }
public DateTime DateChanged { get; internal set; }
public string Username { get; set; }
}
The fact is that UserId, DateCreatedand DateChangedshould never be set through an object, therefore, a keyword internal. Instead, the database will populate these values.
Since objects therefore change as part of the insert operation, I want to return another object IEnumerableof type objects User, but this time with the corresponding properties populated.
I recently realized that I can let Dapper go through objects Userin the IEnumerablefollowing way:
public int Insert(IEnumerable<User> users)
{
string sql = string.Format("INSERT INTO [User] (Username) VALUES (@Username)");
return GetOpenConnection().Execute<User>(sql, users);
}
, foreach. , Execute .
, Query :
public IEnumerable<User> Insert(IEnumerable<User> users)
{
string sql = string.Format("INSERT INTO [User] (Username) VALUES (@Username) SELECT * FROM [User] WHERE UserId = scope_identity()");
return GetOpenConnection().Query<User>(sql, users);
}
InvalidOperationException " (, ..) ".
. ?
IEnumerable, Query ? , IDbTransaction Query , User , Query.
"" Dapper ?