I'm using SQL Server and Dapper and I want to properly store my models object into database and retrieve them.
That's my model, the guid list is list of other model 'Generator' IDs.
public class GeneratorSet
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Guid> GeneratorsList { get; set; }
}
My goal is to correctly map this object to a SQL Server table and then using Dapper correctly retrieve my objects from database. The relationship is many to many (set can 'posses' many generators, and generator may be possesed by many sets).
You can do this using the SplitOn parameter... Here is a guide;
https://taylorhutchison.github.io/2016/03/23/dapper-orm-complex-queries.html
Or just by having unique names and mapping using a lambda - using the example from the documentation (https://github.com/StackExchange/Dapper);
var sql =
@"select * from #Posts p
left join #Users u on u.Id = p.OwnerId
Order by p.Id";
var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});
var post = data.First();
Assert.Equal("Sams Post1", post.Content);
Assert.Equal(1, post.Id);
Assert.Equal("Sam", post.Owner.Name);
Assert.Equal(99, post.Owner.Id);
I can't write the actual code/query as I don't know your database schema... but hopefully you get the idea?