I would like to get the value of @@SERVERNAME
. The following code works:
[Keyless]
public class EntityString
{
public String Value { get; set; } = String.Empty;
};
// inside MyDbContext.OnModelCreating
modelBuilder.Entity<EntityString>();
// inside MyDbContext
public IQueryable<EntityString> QueryServerName()
=> Set<EntityString>().FromSqlRaw("SELECT @@SERVERNAME as Value");
// At usage site
ServerName = (await WHContext.QueryServerName().FirstAsync()).Value
However it seems overly complex for the simple task. Is there an easier way to execute a SELECT
query that is expected to return a single string value?
Using String
instead of EntityString
gave a runtime error that an entity class was required .
2
Answers
The following change worked:
Using
SqlQuery
gave an error thatstring
could not be converted toFormattableString
.Omitting the
as Value
from the query string gave a runtime error"No column name was specified for column 1 of 't'. Invalid column name 'Value'."
You can do something like this