简体   繁体   中英

Error raised when trying to map multiple objects in dapper query

I was testing dapper with a table structure as given in SQL below

    CREATE TABLE [dbo].[Layer](
        [Name] [nvarchar](50) NOT NULL,
        [Key] [nvarchar](255) NULL,
        [FeatureColumn] [nvarchar](255) NULL,
        [Description] [nvarchar](255) NULL,
        [Defaults] [nvarchar](255) NULL,
        [Comments] [nvarchar](255) NULL,
        PRIMARY KEY  ([Name] ASC)
    )

    CREATE TABLE [dbo].[Theme](
        [Name] [nvarchar](50) NOT NULL,
        [IsDefault] [bit] NULL,
        [Field] [nvarchar](255) NULL,
        [Layer] [nvarchar](255) NULL,
        PRIMARY KEY  ([Name] ASC)
    )

SQL for generating data in the tables

    INSERT INTO LAYER 
           (Name  ,[Key]   ,[Description]    ,Defaults,Comments)
    SELECT  'MOJO'  ,'ADM1','Administrative' ,'NULL'    ,'NULL'     UNION ALL
    SELECT  'Roads' ,'LID' ,'Roads'          ,'NULL'    ,'NULL'


    INSERT INTO Theme (Name,IsDefault,Field,Layer)
    SELECT 'M01',1,'ADM1','MOJO'

the POCO objects were Layer and Theme, although I have not mentioned it in the SQL, there is a relation between Layer table and Theme table Theme.Layer -> Layer

    public class Layer 
    {
        public virtual string Name { get; set; }
        public virtual string Key { get; set; }
        public virtual string Description{ get; set; }
        public virtual ICollection<Theme> Themes { get; set; }
        public virtual string Defaults { get; set; }
        public virtual string Comments { get; set; }

        public Layer()
        {
            Themes = new List<Theme>();
        }

    }

    public class Theme 
    {

        public virtual string Name { get; set; }
        public virtual bool IsDefault { get; set; }
        public virtual string Field { get; set; }
        public virtual Layer Layer { get; set; }

        public Theme()
        {
        }

    }

I get the following error when I try to map Layer and Theme in a single query like :

    var sql = @"SELECT * FROM  Layer AS a LEFT OUTER JOIN
    Theme AS b ON a.Name = b.Layer";

    var k = conn.Query<Layer,Theme,  Theme>(
        sql,
        (a, b) => { a.Themes.Add(b); return a ; },
        splitOn: "Name"
        );

The exception raised is

   Error parsing column 9 (Layer=MOJO - String)

Can anyone guide me as to what the issue may be

The issue is that your query returns Theme.Layer as a string but on your model its mapped as Layer class object. One way to fix this would be to change the query to not return that field since its not needed anyways and then update your mapping code to set the Layer property to the layer object returned. Something like:

var sql = @"SELECT a.*, b.Name, b.IsDefault, b.Field
            FROM  Layer AS a 
            LEFT OUTER JOIN Theme AS b ON a.Name = b.Layer";

var k = conn.Query<Layer, Theme, Layer>(sql, (a, b) =>
        {
            if (b != null)
                b.Layer = a;
            a.Themes.Add(b);
            return a;
        }, splitOn: "Name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM