System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.'

введите сюда описание изображенияИспользую процедуру sp_whoisactive для сбора информации о сервере sql. Использую DbQuery WhoIsActive { get; set; } в контексте. Эта модель соответствует столбцам процедуры. Обращаюсь к процедуре через FromSqlRaw и получаю ошибку: System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.'. Возможно что-то с чтением данных, но не могу разобраться, работаю с этим впервые. Пробовал дебажить, но действие в одну строчку, не представляю как найти в ней источник проблемы.

public class WhoIsActive
{
    public short session_id { get; set; }
    public string sql_text { get; set; }
    public string login_name { get; set; }
    public string wait_info { get; set; }
    public string CPU { get; set; }
    public string tempdb_allocations { get; set; }
    public string tempdb_current { get; set; }
    public short blocking_session_id { get; set; }
    public string reads { get; set; }
    public string writes { get; set; }
    public string physical_reads { get; set; }
    public string query_plan { get; set; }
    public string used_memory { get; set; }
    public string status { get; set; }
    public string open_tran_count { get; set; }
    public string percent_complete { get; set; }
    public string host_name { get; set; }
    public string database_name { get; set; }
    public string program_name { get; set; }
    public string additional_info { get; set; }
    public DateTime start_time { get; set; }
    public DateTime login_time { get; set; }
    public int request_id { get; set; }
    public DateTime collection_time { get; set; }
}

public class ApplicationContext : DbContext
{
    public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
    {
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Group> Groups { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<UserGroup> UserGroup { get; set; }
    public DbSet<RoleGroup> RoleGroup { get; set; }
    public DbQuery<WhoIsActive> WhoIsActive { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserGroup>()
            .HasKey(bc => new { bc.UserId, bc.GroupId });
        modelBuilder.Entity<UserGroup>()
            .HasOne(bc => bc.User)
            .WithMany(b => b.UserGroups)
            .HasForeignKey(bc => bc.GroupId);
        modelBuilder.Entity<UserGroup>()
            .HasOne(bc => bc.Group)
            .WithMany(c => c.UserGroups)
            .HasForeignKey(bc => bc.GroupId);

        modelBuilder.Entity<RoleGroup>()
            .HasKey(bc => new { bc.RoleId, bc.GroupId });
        modelBuilder.Entity<RoleGroup>()
            .HasOne(bc => bc.Role)
            .WithMany(b => b.RoleGroups)
            .HasForeignKey(bc => bc.RoleId);
        modelBuilder.Entity<UserGroup>()
            .HasOne(bc => bc.Group)
            .WithMany(c => c.UserGroups)
            .HasForeignKey(bc => bc.GroupId);
    }
}

public class WhoIsActiveRepository : IWhoIsActiveRepository
{
    private readonly ApplicationContext _applicationContext;
    public WhoIsActiveRepository(ApplicationContext applicationContext)
    {
        _applicationContext = applicationContext;
    }

    [Obsolete]
    public IEnumerable<WhoIsActive> GiveServerState()
    {
        var date = _applicationContext.WhoIsActive
            .FromSqlRaw("EXECUTE sp_WhoIsActive @show_sleeping_spids = 2, @show_system_spids = 1, @show_own_spid = 1, @get_additional_info = 1, @get_plans = 1")
            .ToList();

        return date;
    }
}

Кажется я понял свою ошибку в том, что я обращаюсь к applicationContext и пытаюсь в ней вызвать эту процедуру, которая лежит в maset. Но вот вопрос: как выполнить тело процедуры (5к строк) как обычный sql запрос и приконектиться к системной базе данных?


Ответы (0 шт):