C
C#•2mo ago
Indeed

SQL EFCore

Hi! What's current way to query db for data using raw sql? Im used to doing it this way from Doctrinedb and php Something like MyModel[] x = query("SELECT ..."); Rather than using an orm
10 Replies
Indeed
Indeed•2mo ago
Especially for more complex queries
Angius
Angius•2mo ago
Oh, that's easily googleable
Angius
Angius•2mo ago
But if you don't want to use EF... why use EF?
Indeed
Indeed•2mo ago
coming back after a long time to c#, apologies 😅 Was looking for context.Database.SqlQuery<>() I do not really have too much of an idea what other tools are there for such thing
Angius
Angius•2mo ago
Dapper, for example
Jimmacle
Jimmacle•2mo ago
or linq2db if you want to write more advanced queries without dropping all the way to raw SQL
Somgör from Human Resources
Dapper does a great job for what you want to achieve And it's super flexible in terms of what Databases work :soPortuguese:
Indeed
Indeed•2mo ago
Speaking of, Dapper support FormatableSQLStrings? Rider has amazing support for SQL strings but not dapper
Result<int> affected = await Result.Try(() => this._context.GetConnection().ExecuteAsync($"""
INSERT INTO users (username, password)
VALUES (@username, @password)
""", new { username, password = password.ToString() }));
Result<int> affected = await Result.Try(() => this._context.GetConnection().ExecuteAsync($"""
INSERT INTO users (username, password)
VALUES (@username, @password)
""", new { username, password = password.ToString() }));
Result<int> affected = await Result.Try(() => this._context.Database.ExecuteSqlAsync($"""
INSERT INTO users (username, password)
VALUES ({username}, {password.ToString()})
"""));
Result<int> affected = await Result.Try(() => this._context.Database.ExecuteSqlAsync($"""
INSERT INTO users (username, password)
VALUES ({username}, {password.ToString()})
"""));
Indeed
Indeed•2mo ago
No description