I appears to be really easy. If the following is the class definition
[Table("User")]
public class User {
[Column("user_id")]
public Int32 UserId {get;set;}
[Column("user_name")]
public String UserName {get;set;}
[Column("email")]
public String Email {get;set;}
[Column("join_date")]
public DateTime DateAdded {get;set;}
[Column("password")]
public String Password {get;set;}
[Column("last_logged_in")]
public DateTime LastLoggedIn {get;set;}
}
I could just use the following technique
void InsertOrUpdate<T, TUpdateFields, TInsertFields, TKey>(
this DbContext context,
IEnumerable<T> data,
Expression<Func<T, TUpdateFields>> updateFields,
Expression<Func<T, TInsertFields>> insertFields,
Expression<Func<T, TKey>> key){
var sql = context.Set<T>().Select(updateFields).ToString();
// Do stuff
}
eventually the sql variable will contain something like this
SELECT
1 AS [C1],
[Extent1].[user_name] AS [user_name],
[Extent1].[password] AS [password],
[Extent1].[last_logged_in] AS [last_logged_in]
FROM [dbo].[tbUser] AS [Extent1]
This statement can be parsed to find out the table and column names. The columns in the produced sql appears in the same order as they are indicated in the anonymous type