How do you register a Entity Framework Core DbCont...
# help-with-umbraco
a
The documentation hasn't been updated and does not work, also what is shown in the documentation is using an obsolete method, I have taken a look through the source and I can see the tests are doing the following:
Copy code
services.AddUmbracoDbContext<TicketsContext>((serviceProvider, options) =>
            {
   options.UseUmbracoDatabaseProvider(serviceProvider);
});
However when I run the site I get the following error:
Copy code
Unable to create a 'DbContext' of type 'TicketsContext'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[UmbTickets.DAL.DbContexts.Tickets.TicketsContext]' while attempting to activate 'UmbTickets.DAL.DbContexts.Tickets.TicketsContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
https://docs.umbraco.com/umbraco-cms/tutorials/getting-started-with-entity-framework-core#step-2-create-dbcontext-class
d
But I see thats what you did exactly now that I look at it. sorry! is your context similar to this? public ApplicationDbContext(DbContextOptions options) : base(options) { }
Deleted my first comment as it looks like we're doing exactly the same thing.
And looking at the docs you posted is the exact same way I have mine working with UI Builder
The only thing I can think if is that you dont include the --Context in the migrations part
l
do you get this when you run the project? Or are you using the
dotnet ef migrations add
command? If you use the command, you should be able to specify the migrated project, like
dotnet ef migrations add --context TicketsContext --project .\path\to\your\DbContext.csproj
a
The donet migrations command, I'll give that ago when I next get a chance 👍 Thanks!
@Liam nope same issue:
j
Can you share your TicketContext class?
l
When I'm doing these migrations, I need to be in the web application library when I execute the command. For a project with the structure:
Copy code
-> \MyUmbracoSln
------> \MyUmbracoSln.Umbraco.Site    // contains the front-end
------> \MyUmbracoSln.Umbraco.Core
------> \MyUmbracoSln.Database        // contains the EF Contexts
My steps go:
cd .\MyUmbracoSln.Umbraco.Site
Then :
dotnet ef migrations add InitialCreate --context MyEntityContext -o .\Generated\ --project ..\MyUmbracoSln.Database\MyUmbracoSln.Database.csproj
j
You can do it from the ef core project as well, and then add a
-s
option with a path to the website project instead
l
that's neat, I didn't know about the
-s
option
46 Views