ASP.NET EF help
Can anyone help me with this , I dont know why 'usesqlserver' is underlined while I have entityframework installed .
53 Replies
The providers have their own library, SQLServer included
This is the one you want probably https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/
Microsoft.EntityFrameworkCore.SqlServer 8.0.4
Microsoft SQL Server database provider for Entity Framework Core.
I think u need Core, SqlServer, Tools for it
No, he just needs to install
Microsoft.EntityFrameworkCore.SqlServer
, which is what I mentioned alreadyit worked bro thank you so much
He still needs to add this constructor
where do i hvae to write this?
If u use migrations
You absolutely do not need a parameterless constructor
if you intend on using Entityframeworkcore you need to have a parameterless constructor if you wanna create migrations as far as im concerned, but if you have an alternative id like to learn from it:)
Like this my man
I use EF Core daily, and have created many migrations. You do not need a paramterless constructor for your context.
you just need the options one. it even works for console apps, doesnt need to be webapi
Sounds weird because i got some issues when is was trying to add new migration without a parameterless constructor else can u try showing me how u would do it?
There really isnt anything to it, you just have the normal options ctor, and your app must use the generic host builder iirc
and now the
dotnet ef
commands work just fine
here is an example from a demo app I have
so in theory i would need my parameterless constructor in my dbcontext here:
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
That is an unholy OnConfiguring
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
You don't need this
Oh my chat was scrolled up
Completely unneeded, especially since the question was about a compiler error
It would only be required when the class has to implement some generic constraint that forces it, but a context works with DI which is perfectly capable of having parameters
Also, please register a proper logger instead of using
UseLoggerFactory
like that
That setup is questionable
If you just use a proper setup in the root service collection then the context will work with that by default
Unless you have a specific reason for a custom system in thismy teacher made it like that haha so i just assumed it was correct
Oof, common bad teacher issue
I assume you have a configured service collection you add the context to?
yeah.. hes not really much of a teacher its mostly self learning
Yeah I hear that often
😛
im not sure what you mean by this so probably not haha
Is this a web api?
Do you have a
startup.cs
file?blazor webassembly
no
That one has it too, but if you use webassembly then you also need a server
Otherwise you have a client application that communicates with a database, which makes no sense
So now I'm confused
ill show you my solution
So you do have an API
yeah
Surely the context has to work for the API then?
Can you expand the API?
Ok you use a minimal API then
In
Program.cs
you set up the service collection
That's where you have AddDbContext<>();
oh you meant lke that haha
In there you can just set up a proper logger, using Serilog for example, and the context will use that by default
ill look into it
This is my Serilog setup for a site I am hosting
Then my database context is just this
Like this?
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
logging to txt file :PP
But even this is completely optional
How come? I didn't realize that
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
Good one. Would there be a way to provide a logger without it?
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
Pretty sure this was required for the logger to be picked up. Otherwise it didn't work or would overlap with another one or something, but it has been a while since I even looked into that
Could be wrong
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
I believe that was my reason yes
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View