I have 2 issues, hopefully both can be answered in this question. I have a model like this:
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } }
Now assume that my Stored Procedure (which can't be changed) returns first_name, lastName and UserEmail. I would like to use some method (hopefully data annotations) to map the fields to the properties. Something like this:
public class Person { [Column(Name = "first_name")] public string FirstName { get; set; } [Column(Name = "lastName")] public string LastName { get; set; } [Column(Name = "UserEmail")] public string Email { get; set; } }
Is this possible? You may suggest making sure properties match the field names because they are not case sensitive, so changing Email to UserEmail would work, but it isn't for this reason. I have a class that has 2 Person assigned to it:
public class Example { public string Name { get; set; } public Person Mother { get; set; } public Person Father { get; set; } }
Also, I can't use:
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
because it seems to ignore all other properties then. Any help would be greatly appreciated as usual :)
You can do it with Chain.
static Tortuga.Chain.SqlServerDataSource DS = new Tortuga.Chain.SqlServerDataSource (connectionString); var people = DS.Procedure("procName", new { @Param1 = x, @Parm2 = y}).ToCollection<Person>().Execute();
It honors the
Column attributes by default.
Your second example is harder. You need prefixes so that it knows which FirstName goes with which property.
public class Example { public string Name { get; set; } [Decompose("Mother_")] public Person Mother { get; set; } [Decompose("Father_")] public Person Father { get; set; } } SELECT x.nName, m.FirstName as Mother_FirstName, f.FirstName as Father_FirstName, [...] FROM people x LEFT JOIN people f ON x.FatherKey = f.Key LEFT JOIN people m ON x.MotherKey = m.Key