首页 > 解决方案 > 我在 asp.net C# MVC 中使用 dapper 映射数据时出错

问题描述

这是错误:CS1061 C# 不包含“查询”的定义,并且找不到接受第一个类型参数的可访问扩展方法“查询”(您是否缺少 using 指令或程序集引用?)

这是我的模型

public class TourPackage
{
    public int p_id { get; set; }
    public string p_name { get; set; }
    public string p_price { get; set; }
    public string p_category { get; set; }
    public string p_bedroom { get; set; }
    public List<InclusionList> inclusionLists { get; set; }
    public List<ConditionList> conditionLists { get; set; }
    public List<Exclusions> exclusiions { get; set; }
    public List<Flights> flights { get; set; }
    public List<Itineray> itinerays { get; set; }
    public List<TDates> dates { get; set; }
    public List<sliders> sliders { get; set; }
    public List<Visareq> visareqs { get; set; }
}

public class InclusionList
{ 
    public int in_id { get; set; }
    public int p_id { get; set; }
    public string in_name { get; set; }
}

public class ConditionList
{
    public int co_id { get; set; }
    public int p_id { get; set; }
    public string co_name { get; set; }
}

public class Exclusions
{
    public int ex_id { get; set; }
    public int p_id { get; set; }
    public string ex_name { get; set; }
}

public class Flights
{
    public int fl_id { get; set; }
    public int p_id { get; set; }
    public string fl_name { get; set; }
}

public class Itineray
{
    public int it_id { get; set; }
    public int p_id { get; set; }
    public string it_name { get; set; }
    public int bmeal { get; set; }
    public int lmeal { get; set; }
    public int dmeal { get; set; }
}

public class TDates
{
    public int td_id { get; set; }
    public int p_id { get; set; }
    public string td_fdate { get; set; }
    public string td_tdate { get; set; }
}

public class sliders
{
    public int img_id { get; set; }
    public int p_id { get; set; }
    public string img_slides { get; set; }
}

public class Visareq
{
    public int vi_id { get; set; }
    public string vi_name { get; set; }
}



 public class Bedtype
    {
        public int bed_id { get; set; }
        public string bedtype { get; set; }
    }

 public class Category
    {
        public int c_id { get; set; }
        public string c_type { get; set; }
    }

这是我试过的查询

        public static List<TourPackage> LoadPackageData()
    {
        using (IDbConnection con = new SqlConnection(GetConnectionString()))
        {
            string sql = @"select pt.p_id, pt.bedroom, pt.c_id, pt.p_name, pt.p_price, 
            br.bedtype, 
            ca.c_type,
            inc.name,
            co.name,
            ex.name,
            fl.name,
            it.name, it.description, it.bmeal, it.lmeal, it.dmeal,
            tr.dfrom, tr.dto,
            vi.name,
            img.slides  
            from dbo.t_package as pt
            left join bedrooms as br on pt.bedroom = br.bed_id
            left join category as ca on pt.c_id = ca.c_id
            left join inclusion as inc on pt.p_id = inc.p_id
            left join condition as co on pt.p_id = co.p_id
            left join exclusion as ex on pt.p_id = ex.p_id
            left join flightdetails as fl on pt.p_id = fl.p_id
            left join itinerary as it on pt.p_id = it.p_id
            left join traveldates as tr on pt.p_id = tr.p_id
            left join visareq as vi on pt.p_id = vi.p_id
            left join imagetable as img on pt.p_id = img.p_id
            order by pt.p_id;"; 
            var query = con.Query<TourPackage, Bedtype, Category, InclusionList, ConditionList, Exclusions, Flights, Itineray, TDates, sliders, Visareq, TourPackage>(sql,
                (tr, bt, ca, inc, co, ex, fl, it, td, sl, vi) => 
                {
                    tr.bedroom = bt;
                    tr.c_id = ca;
                    tr.p_id = inc;
                    tr.p_id = co;
                    tr.p_id = ex;
                    tr.p_id = fl;
                    tr.p_id = it;
                    tr.p_id = td;
                    tr.p_id = sl;
                    tr.p_id = vi;
                    return tr;
                }, splitOn: "bed_id, c_id, p_id, p_id, p_id, p_id, p_id, p_id, p_id, p_id");
            return query;

        }
    }

我尝试了很多解决方案,但错误仍然存​​在。

这是我的数据库表

    create table bedrooms (
    bed_id int primary key identity(1,1) not null,
    bedtype varchar(50) not null
);

create  table category (
    c_id int identity(1,1) primary key not null,
    c_type varchar(100) not null
);

create table t_package (
    p_id int primary key identity(1,1) not null,
    p_name varchar(100) not null,
    p_price float,
    c_id int,
    foreign key (c_id) references category(c_id),
    bedroom int,
    foreign key(bedroom) references bedrooms(bed_id)
);

create  table inclusion (
    in_id int identity(1,1) primary key not null,
    name varchar(150) not null,
    p_id int,
    foreign key (p_id) references t_package(p_id)
);

create table condition (
    con_id int primary key identity(1,1) not null,
    name varchar(150) not null,
    p_id int,
    foreign key (p_id) references t_package(p_id)
); 

create table exclusion (
    ex_id int primary key identity(1,1) not null,
    name varchar(150) not null,
    p_id int,
    foreign key(p_id) references t_package(p_id)
);

create table flightdetails (
    fl_id int primary key identity(1,1) not null,
    name varchar(100) not null,
    p_id int,
    foreign key (p_id) references t_package(p_id)
);

create table itinerary (
    it_id int primary key identity(1,1) not null,
    name varchar(100) not null,
    description text,
    bmeal int,
    lmeal int,
    dmeal int,
    p_id int,
    foreign key (p_id) references t_package(p_id)
);

create table traveldates (
    tdates_id int primary key identity(1,1) not null,
    dfrom date,
    dto date,
    p_id int,
    foreign key (p_id) references t_package(p_id)
);

create table imagetable (
    img_id int primary key identity(1,1) not null,
    img_header varchar(500),
    img_sliders text,
    p_id int,
    foreign key (p_id) references t_package(p_id)
);

create table visareq 
(
    visa_id int primary key identity(1,1) not null,
    name varchar(150),
    p_id int,
    foreign key (p_id) references t_package(p_id)
);

这是我的包裹数据的图像,这只是一个包裹,但有一个详细信息列表,如行程等。

在此处输入图像描述

我制作了一个单独的文件,即 Query.cs 作为我的查询容器,但是当我进行多表映射时遇到了这个错误,这就是为什么我决定将我的多表映射放在我的 Connect.cs 中的 LoadPackageData() 方法中,但错误仍然存​​在。

这是我的 Connect.cs

  using Dapper;
using DataLibrary.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataLibrary.Connection
{
    public class Connect
    {
        public static string GetConnectionString(string connection = "mycon")
        {
            return ConfigurationManager.ConnectionStrings[connection].ConnectionString;
        }

        public static int SaveBedType<B>(string sql, B data)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                return con.Execute(sql, data);
            }
        }

        public static int SaveSlide<S>(string sql, S data)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                return con.Execute(sql, data);
            }
        }

        public static int SaveCategory<C>(string sql, C data)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                return con.Execute(sql, data);
            }
        }

        public static List<B> LoadData<B>(string sql)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                return con.Query<B>(sql).ToList();
            }
        }

        public static List<DDP> LoadDDPackage<DDP>(string sql)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                return con.Query<DDP>(sql).ToList();
            }
        }

        //public static List<TP> LoadPackageData<TP, INC, CO, EX, FL, IT, TD, SL, VI, RS>(string sql, object param)
        //{
        //    using (IDbConnection con = new SqlConnection(GetConnectionString()))
        //    {
        //        var q = con.Query<TP, INC, CO, EX, FL, IT, TD, SL, VI, RS>(sql,
        //            (tr, inc, co, ex, fl, it, td, sl, vi, tl) => { tr.p_id = inc; return tr;  });
        //        return q;
        //    }
        //}

        public static List<TourPackage> LoadPackageData()
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                const string sql = @"select pt.p_id, pt.bedroom, pt.c_id, pt.p_name, pt.p_price, 
                br.bedtype, 
                ca.c_type,
                inc.name,
                co.name,
                ex.name,
                fl.name,
                it.name, it.description, it.bmeal, it.lmeal, it.dmeal,
                tr.dfrom, tr.dto,
                vi.name,
                img.slides  
                from dbo.t_package as pt
                left join bedrooms as br on pt.bedroom = br.bed_id
                left join category as ca on pt.c_id = ca.c_id
                left join inclusion as inc on pt.p_id = inc.p_id
                left join condition as co on pt.p_id = co.p_id
                left join exclusion as ex on pt.p_id = ex.p_id
                left join flightdetails as fl on pt.p_id = fl.p_id
                left join itinerary as it on pt.p_id = it.p_id
                left join traveldates as tr on pt.p_id = tr.p_id
                left join visareq as vi on pt.p_id = vi.p_id
                left join imagetable as img on pt.p_id = img.p_id
                order by pt.p_id;"; 
                var query = con.Query<TourPackage, Bedtype, Category, InclusionList, ConditionList, Exclusions, Flights, Itineray, TDates, sliders, Visareq, TourPackage>(sql,
                    (tr, bt, ca, inc, co, ex, fl, it, td, sl, vi) => 
                    {
                        tr.bedroom = bt;
                        tr.c_id = ca;
                        tr.p_id = inc;
                        tr.p_id = co;
                        tr.p_id = ex;
                        tr.p_id = fl;
                        tr.p_id = it;
                        tr.p_id = td;
                        tr.p_id = sl;
                        tr.p_id = vi;
                        return tr;
                    }, splitOn: "bed_id, c_id, p_id, p_id, p_id, p_id, p_id, p_id, p_id, p_id");
                return query;

            }
        }

        public static List<C> LoadCategory<C>(string sql)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                return con.Query<C>(sql).ToList();
            }
        }

       public static int SavePackage(string sql, DynamicParameters p)
       {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                con.Execute(sql, p);
                return p.Get<int>("@Id");
            }
       }

       public static int SaveTDates(DataTable d)
       {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                var dates = new
                {
                    travelDates = d.AsTableValuedParameter("DateUDT")
                };
                return con.Execute("dbo.TravelDateInsert", dates, commandType: CommandType.StoredProcedure);
            }
       } 

        public static int SaveItinerary(DataTable i)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                var details = new
                {
                    itinerary = i.AsTableValuedParameter("MyUDT")
                };
                return con.Execute("dbo.Itinerary_insert", details, commandType: CommandType.StoredProcedure);
            }
        }

        public static int SaveFlightDetails(DataTable f)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                var flights = new
                {
                    flightDetails = f.AsTableValuedParameter("FlightDUDT")
                };
                return con.Execute("dbo.FlightDetails_insert", flights, commandType: CommandType.StoredProcedure);
            }
        }

        public static int SaveConditions(DataTable tc)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                var condition = new
                {
                    terms_conditions = tc.AsTableValuedParameter("ConditionUDT")
                };
                return con.Execute("dbo.Condition_insert", condition, commandType: CommandType.StoredProcedure);
            }
        }

        public static int SaveExclusion(DataTable e)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                var exclusion = new
                {
                    exclusions = e.AsTableValuedParameter("ExclusionUDT")
                };
                return con.Execute("dbo.Exclusion_insert", exclusion, commandType: CommandType.StoredProcedure);
            }
        }

        public static int SaveVisareq(DataTable v)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                var visa = new
                {
                    visareq = v.AsTableValuedParameter("VisaReqUDT")
                };
                return con.Execute("dbo.VisaReq_insert", visa, commandType: CommandType.StoredProcedure);
            }
        }

        public static int SaveInclusion(DataTable _in)
        {
            using (IDbConnection con = new SqlConnection(GetConnectionString()))
            {
                var inclusion = new
                {
                    inclusions = _in.AsTableValuedParameter("InclusionUDT")
                };
                return con.Execute("dbo.Inclusion_insert", inclusion, commandType:CommandType.StoredProcedure);
            }
        }
    }
}

标签: c#asp.net-mvcdapper

解决方案


你有太多的泛型参数。大多数参数的查询重载是这个:

public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) =>
            MultiMap<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(cnn, sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);

所以,你只能有 7 和返回类型。您可以在此处找到源代码。


推荐阅读