You need to run a query using the Entity Framework Core:

SELECT to_seconds(now()) - to_seconds(lastaccess) as seconds FROM survey WHERE id=@param 

This code does not work:

 var i = _db.Database.FromSql<int>( @"SELECT to_seconds(now()) - to_seconds(lastaccess) as seconds FROM survey WHERE id=@param;", @param).ToList(); 

Writes that 'DatabaseFacade "does not contain a definition for" FromSql "

I tried to turn this field into an entity by making it NotMapped:

 entity.Property(e =>e.LastAccessComputed) .HasComputedColumnSql("to_seconds(now()) - to_seconds(lastaccess)"); 

In this case, the base swears, because she searches for a field in the table.

How to get the result of such a query?

  • Once a single entry is not it easier to pull it out and get a period through DateTime? - tcpack4
  • I have another time on the server, and there is a lot to be done in the future to make such calculations using pseudonyms. - Lydia

1 answer 1

The problem is in the version. In EF Core 1.0, you can write the database connection from EF.

 var conn = _context.Database.GetDbConnection(); try { await conn.OpenAsync(); using (var command = conn.CreateCommand()) { string query = "SELECT EnrollmentDate, COUNT(*) AS StudentCount " + "FROM Person " + "WHERE Discriminator = 'Student' " + "GROUP BY EnrollmentDate"; command.CommandText = query; DbDataReader reader = await command.ExecuteReaderAsync(); if (reader.HasRows) { while (await reader.ReadAsync()) { var row = new EnrollmentDateGroup { EnrollmentDate = reader.GetDateTime(0), StudentCount = reader.GetInt32(1) }; groups.Add(row); } } reader.Dispose(); } } finally { conn.Close(); }