实体框架- dbcontext数据库sqlquery连接多个表在SQLite

本文关键字:SQLite 连接 sqlquery 框架 dbcontext 数据库 实体 | 更新日期: 2023-09-27 18:17:51

我不能执行SQL QUERY(string QUERY)错误信息:数据阅读器有多个字段。多个字段对EDM基本类型无效。如何修复????

Department class code here:
public class Department   
{
    [Key]
    public Int64 DepartmentID { get; set; }
    public string DepartmentName { get; set; }
    public Int64 P_ID { get; set; }
}   
Person class code here:
public class PersonInfo    
{       
    [Key]
    public Int64 P_ID { get; set; }
    public string PersonName { get; set; }
    public string Register_Num { get; set; }
}
using (var db = new EntityContext())
        {
            try
            {
                int result = 0;
                string query = @"
                     SELECT P.P_ID, D.DepartmentName FROM PersonInfo AS P
                     INNER JOIN Department AS D ON P.P_ID = D.P_ID";
                var exists = db.Database
                .SqlQuery<Department>(query).ToList();
                foreach (var item in exists)
                {
                    MessageBox.Show(string.Format("{0}", item));
                }                    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.InnerException.Message);
            }
        } 

实体框架- dbcontext数据库sqlquery连接多个表在SQLite

当您尝试将结果集映射到Department Model类时,因此您需要在sql查询中返回departmentd。请注意,所有的列名应该与您的部门类的属性完全匹配。