映射

时间:2016-12-23 08:02:39

标签: dapper

我在下面的代码中面临问题。一切看起来都很好,但我仍然低于错误:

  

'MetalItems'不包含'metals'的定义,并且没有扩展方法'metals'接受'MetalItems'类型的第一个参数(你是否缺少using指令或汇编引用?)E:\ VS2010 \ Jewellery \ SilverSpoon \ SilverSpoon \ API \ models \ MetalItems.cs 38 52 SilverSpoon

CODE

class MetalItems
{
    public int ID { get; set; }
    public int metal_id { get; set; }
    public string item_name { get; set; }
    public Metals metals {get; set;}

    public IEnumerable<MetalItems> findAllJoin<MetalItems>()
    {
        IEnumerable<MetalItems> data = null;

        using (var sqlConnection = Database.getConnection())
        {
            data = sqlConnection
                .Query<MetalItems, Metals, MetalItems>(
                @"SELECT * FROM metal_items mi JOIN metals m ON mi.metal_id = m.metal_id ORDER BY mi.metal_id",
                                        (mi, m) =>
                                        {
                                            mi.metals = m;
                                            return mi;
                                        }, 
                                        splitOn: "metal_id");
        }
        return data;
    }
}

class Metals
{
    public int metal_id { get; set; }
    public string metal_name { get; set; }
    public decimal rate_per_gram { get; set; }
    public decimal making_charges { get; set; }
}

ERROR LINE 显示在以下快照中;

enter image description here

1 个答案:

答案 0 :(得分:1)

一般来说,我并没有为其中包含数据操作的类提供足够的资金,他们更愿意将这个逻辑分离到它自己的数据层,但如果这是你想要的......

只需删除&#34;泛型&#34;?你的findAllJoin方法的一部分,它应该工作得很好

        public IEnumerable<MetalItems> findAllJoin()
        {
            IEnumerable<MetalItems> data = null;

            using (var sqlConnection = Database.getConnection())
            {
                data = sqlConnection
                    .Query<MetalItems, Metals, MetalItems>(
                    @"SELECT * FROM metal_items mi JOIN metals m ON mi.metal_id = m.metal_id ORDER BY mi.metal_id",
                                            (mi, m) =>
                                            {
                                                mi.metals = m;
                                                return mi;
                                            },
                                            splitOn: "metal_id");
            }
            return data;
        }