如何在dapper.net中创建正确的多映射查询

时间:2017-02-27 19:31:22

标签: c# mysql dapper

我正在创建简单的应用程序,这有助于组织我的口味混合自己的电子液体。我在localDb中有三个表:

香精:

ID,姓名,公司,商店,金额

配方:

ID,姓名,日期

RecipeFlavours(将风味和食谱连接成一对多)

Id,FlavourId,RecipeId,PercentageAmount

我也提供了模特:

香精:

    public int Id { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Shop { get; set; }
    public double? Amount { get; set; }

配方:

    public string Name { get; set; }
    public DateTime Date { get; set; }
    public int Id { get; set; }
    public Dictionary<Flavour, double?> RecipeElements { get; set; }

RecipeElement:

public int Id { get; set; }
public Flavour Flavour { get; set; }
public double? PercentageAmount { get; set; }

我执行查询以从数据库中获取所有食谱:

    public IEnumerable<Recipe> GetAll(){
        string sqlQuery = @"SELECT f.*, r.*, rf.PercentageAmount FROM Recipe r INNER JOIN RecipeFlavours rf ON rf.RecipeId = r.Id INNER JOIN Flavour f ON rf.FlavourId = f.Id";
        var lookup = new Dictionary<int, Recipe>();
        Db.Query<Recipe, Flavour,ElementRecipe, Recipe>(sqlQuery, (r, f, rf) =>{
            Recipe recipe;
            if(!lookup.TryGetValue(r.Id,out recipe))
                lookup.Add(r.Id, recipe = r);
            if(r.RecipeElements == null)
                recipe.RecipeElements = new Dictionary<Flavour, double?>();
            recipe.RecipeElements.Add(f,rf.PercentageAmount);
            return recipe;

        },splitOn: "RecipeId, FlavourId, Id, Id, Id");



        return lookup.Values.ToList();
    }

执行查询时,我得到例外:

An unhandled exception of type 'System.ArgumentException' occurred in Dapper.dll

Additional information: When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id

我无法识别我做错了什么。我在这个网站上学习:http://jsm85.github.io/tutorial/2016/04/13/map-multi-join-sql-query-using-dapper/

0 个答案:

没有答案