C
C#2d ago
Faker

✅ Why the need to create a database using EF core and not do it manually using SQL

Hello guys, I'm currently learning the EF core so that I can connect with my database using SQL Server. I have a question. I noticed that if we want to create a database using EF core, we need to create a dbContext, a specific instance, like a Student class with its properties etc. My question is, is all that required? Can't we not just use SQL statement to create all that using SQL Server? Isn't it easier? Or there is use cases of when we absolutely need to use C# code?
54 Replies
Angius
Angius2d ago
You need a way to create the queries somehow
mg
mg2d ago
When you do code first like that, EF ultimately generates SQL code to create the database and tables for you SQL is always how you'll communicate with the database
Angius
Angius2d ago
If you don't have a Person with a name, you can't have a DbSet<Person> so you can't reference the table
mg
mg2d ago
EF is a library provided to make working with it from C# easier
Angius
Angius2d ago
And you can't query by name with .Where(p => p.Name == name)
Jimmacle
Jimmacle2d ago
the entire point of ORMs like EF Core is so you can interact with the database using type-safe C# code, there is nothing stopping you from using raw SQL or something more minimal like dapper
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP2d ago
ah, we can't create the queries without the dbContext? (I haven't try it yet)
Angius
Angius2d ago
You can just rawdog SQL, but then there's no point using EF and you should just go with Dapper
mg
mg2d ago
you can create a query however you want
Jimmacle
Jimmacle2d ago
you can also go with Linq2DB if you want type-safe SQL without the change tracking and hand-holding that EF has
mg
mg2d ago
select * from Persons
select * from Persons
done
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
mg
mg2d ago
EF also has methods like context.Database.SqlQuery() that allow you to write your own SQL and send it to the database
Angius
Angius2d ago
True
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle2d ago
if you really want you can create your database with raw SQL and scaffold a dbcontext from there, but if it's a new application then taking advantage of EF migrations is usually more convenient
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Steve
Steve2d ago
EF is nice for migrations. Just run the migration to set up the database in a new environment. Boom. Now you don't have to go through the pain of manually running SQL commands
Faker
FakerOP2d ago
I often saw the term "Code first approach"; this is when we use the dbContext to create the database schema ?
Jimmacle
Jimmacle2d ago
yes
mg
mg2d ago
yes
Steve
Steve2d ago
yes
Jimmacle
Jimmacle2d ago
the opposite would be database first, using the database schema to create the dbcontext
Faker
FakerOP2d ago
ok ok starts to make sense, will continue to read 2sec
Jimmacle
Jimmacle2d ago
code first you basically just change your C# models then tell EF "figure out what i changed and generate SQL to change the schema the same way"
Faker
FakerOP2d ago
yeah, this is where migrations and all came into play?
Steve
Steve2d ago
That is so helpful.
Jimmacle
Jimmacle2d ago
until you have to write migrations by hand :when:
Sehra
Sehra2d ago
you can also ask efcore to generate a sql script for you
Jimmacle
Jimmacle2d ago
which you don't 99% of the time, but i happen to be doing that right now because EF doesn't support columnstore tables directly
Faker
FakerOP2d ago
hmm the thing is, in order to create queries, we need to have a context?
mg
mg2d ago
yeah i like EF because 99% of the time i can do things the EF way but then it also gives you escape hatches for the 1%
Angius
Angius2d ago
ye
Faker
FakerOP2d ago
irrespective of whether we are using code first or we are scaffolding ?
Angius
Angius2d ago
ye When you scaffold, EF will generate the context from the database schema And all the models With code-first, it's the reverse You create models and context, and EF creates he db schema
Faker
FakerOP2d ago
yeah I see
Jimmacle
Jimmacle2d ago
with EF you always need a dbcontext, that's just how you use EF that object represents the database in your code
mg
mg2d ago
it's worth noting that the whole point of using EF is to create queries and map their results for you. it's not so much that a context is required to write queries, it's that you're using EF to write queries for you
Jimmacle
Jimmacle2d ago
i've been using EF primarily with linq2db on top for things EF doesn't support like merge queries
mg
mg2d ago
even the term "writing queries" is a little ambiguous here because (most of the time) you won't be writing SQL, you'll be writing LINQ that EF turns into SQL i need to give linq2db another look
Angius
Angius2d ago
Yeah, it's more like translating queries than writing
Faker
FakerOP2d ago
Yeah I see, so to recap: In order to use EF Core, we need a dbContext, irrespective of whether we use the code first approach (code first approach is where we create the model and everything related, its context, properties, etc using code) or database first approach (use sql statements to create database directly in sql server). The thing is when we use the database first approach, we might introduce typos, which we don't want and this may be avoided. Now if we use the database first approach, we still need a context to be used, their is where we need to scaffold the database so that the model/classes are created automatically for us and we can use that to write our LINQ expressions. can anyone just confirm if I have correctly understood based on this summary pls
Jimmacle
Jimmacle2d ago
pretty much
Angius
Angius2d ago
Yeah
Faker
FakerOP2d ago
noted, thanks, I learn a lot right now, really appreciate all the responses 👍 by the way linq2db is same thing as LINQ?
Jimmacle
Jimmacle2d ago
no it's a different ORM
Jimmacle
Jimmacle2d ago
GitHub
GitHub - linq2db/linq2db: Linq to database provider.
Linq to database provider. Contribute to linq2db/linq2db development by creating an account on GitHub.
Jimmacle
Jimmacle2d ago
but it can be used alongside EF if you need to make queries that EF doesn't support, using EF's model
Angius
Angius2d ago
// this throws on runtime
var p = await _ctx.ExecuteQueryAsync<Person>("SELECT * FROM Peolpe");

// WEE-WOO WEE-WOO PROPERTY `Peolpe` DOES NOT EXIST ON `MyCoolContext`!
var p = await _ctx.Peolpe.ToListAsync();
// this throws on runtime
var p = await _ctx.ExecuteQueryAsync<Person>("SELECT * FROM Peolpe");

// WEE-WOO WEE-WOO PROPERTY `Peolpe` DOES NOT EXIST ON `MyCoolContext`!
var p = await _ctx.Peolpe.ToListAsync();
Faker
FakerOP2d ago
oh ok I see
Angius
Angius2d ago
Linq2db can even be kinda bolted on top of EF, where it basically just adds the missing bits
Jimmacle
Jimmacle2d ago
linq2db doesn't have migrations or change tracking built in, and the queries you write are much closer to raw SQL (but still type-safe C#) than in EF which is more like your typical C# LINQ
Faker
FakerOP2d ago
yep I see, will just get started with EF core for now Thanks guys ! 💯

Did you find this page helpful?