Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回空值的DbQuery_C#_Sql Server_Ef Core 2.2_.net Core 2.2 - Fatal编程技术网

C# 返回空值的DbQuery

C# 返回空值的DbQuery,c#,sql-server,ef-core-2.2,.net-core-2.2,C#,Sql Server,Ef Core 2.2,.net Core 2.2,使用.NET核心2.2.300和实体框架核心2.2.4 我在SQL Server数据库中得到了两个内容相似的表,因此我编写了一个查询,如下所示: SELECT A.guid_id AS Id , A.document AS Document FROM TableA A WHERE A.guid_user_id = 'valid guid in here' UNION ALL SELECT B.guid_id AS Id , B.user_document

使用.NET核心2.2.300和实体框架核心2.2.4

我在SQL Server数据库中得到了两个内容相似的表,因此我编写了一个查询,如下所示:

SELECT
    A.guid_id AS Id
    , A.document AS Document
FROM TableA A
WHERE
    A.guid_user_id = 'valid guid in here'
UNION ALL
SELECT
    B.guid_id AS Id
    , B.user_document AS Document
FROM TableB B
WHERE
    B.guid_user_id = 'valid guid in here'
当我运行它时,它会工作,并带来我需要的结果。列类型也匹配。 我在这个C项目上得到了一个类似这样的课程:

using System;

namespace MyProject.Domain.Entities
{
    public class UserDocument
    {
        public Guid? Id;
        public string Document;
    }
}
对于我的DbContext,我得到了以下类:

...
public class MyDbContext : DbContext
    {
        private readonly IHostingEnvironment _env;
        public MyDbContext(IHostingEnvironment env)
        {
            _env = env;
        } 

        ... some DbSets in here that are working fine ...
        public DbQuery<UserDocument> UserDocuments { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            ... a couple "modelBuilder.ApplyConfiguration(config instance);" for the DbSets ...

            base.OnModelCreating(modelBuilder);
        }
    }
在我的存储库类上:

public IEnumerable<UserDocument> GetUserDocuments(string accountId)
        {
            string query = $@"SELECT
                                  A.guid_id AS Id
                                  , A.document AS Document
                              FROM TableA A
                              WHERE
                                  A.guid_user_id = '{accountId}'
                              UNION ALL
                              SELECT
                                  B.guid_id AS Id
                                  , B.user_document AS Document
                              FROM TableB B
                              WHERE
                                  B.guid_user_id = '{accountId}';";
            #pragma warning disable EF1000 // Possible SQL injection vulnerability, I know, but it is just for testing.
            return Db.UserDocuments.FromSql(query).ToList(); // Db holds an instance of MyDbContext
            #pragma warning restore EF1000
        }
它运行并返回它应该返回的正确数量的对象,但所有这些对象都得到了空值的属性

我错过什么了吗

我正在寻找一种获得预期结果的方法,而不必为TableA和TableB创建数据库集来创建联合。有办法吗


我的数据库集一切都很好,但这个数据库查询就是不起作用。

我只是忘了在模型中添加getter和setter。它看起来就像是在工作:

using System;

namespace MyProject.Domain.Entities
{
    public class UserDocument
    {
        public Guid? Id { get; set;}
        public string Document { get; set;}
    }
}

我只是忘了在模型中添加getter和setter。它看起来就像是在工作:

using System;

namespace MyProject.Domain.Entities
{
    public class UserDocument
    {
        public Guid? Id { get; set;}
        public string Document { get; set;}
    }
}

使用属性,而不是字段。或者,读一读遗嘱,我不敢相信我错过了。谢谢,伙计。使用属性,而不是字段。或者,读一读遗嘱,我不敢相信我错过了。谢谢,伙计。