Unable to create an object of type 'BlogContext'. For the different patterns supported at design tim...
s
Unable to create an object of type 'BlogContext'. For the different patterns supported at design time, see
d
Usually you'd get this error if your context has constructor parameters. The fact that you're defining it in an RCL should not affect anything
The link in that error message is quite informative, if you haven't checked it out yet, I highly recommend you do.
s
I did check it out but I just followed exactly the documentation from umbraco site. So I changed nothing but the name of my Class and I stripped only to id and name column
could it then have something to do that I use sqlserver? or maybe that I have AddUmbracoEFCoreContext in the Compose and not in startup.cs ?
added -v for extra info
Finding Microsoft.Extensions.Hosting service provider... No static method 'CreateHostBuilder(string[])' was found on class 'Program'. No application service provider was found.
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[FontAwesome5Picker.Data.S4aContext]' while attempting to activate 'FontAwesome5Picker.Data.S4aContext'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.b__13() --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.b__13() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
d
Ah, that's a good error message. The error says it couldn't find a static method 'CreateHostBuilder'. It takes that static method as a starting point for generating your migration. Since you're in a razor class library, I don't expect that method to be found. I also don't expect your class library to have a Program class. On which project are you running your migration command?
Does your context have a parameterless constructor? If it does, the design-time migration command should be able to resolve it as well. Alternatively, you can use a design-time factory to create an instance of your context when generating migrations
s
true my RCL doesn't have a program class also no startup as I am trying to create a package. I am trying to run the Migration on my package project (so not the umbraco site project)
I want my package on install to add the table in the umbraco database
"Does your context have a parameterless constructor?" << I am just following the Umbraco docs here so I guess this is what you are asking for?
d
Ah, that's not a parameterless constructor 😅 .
I know nothing of the Umbraco docs, when it comes to entity framework, I rather rely on Microsoft.
Anyway, You likely need to create a design time factory to make it work
Here you can see how to do that:https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory
s
oke will dive into this but strange that just following the umbraco docs does not get you there
d
The docs likely work just fine if you use EF Core inside a website project. You, on the other hand, are working in a package, therefore, the rules are slightly different
s
Thank you very much with helping here!
I will dive into the link you provided
worst case is adding the migration files manually
d
oh I'm sure that won't be necessary
s
🙂
sorry 1 more question on creating from design time factory
my Context has a required member (still following docs 🙂 )public required DbSet s4aProducten { get; set; }
d
ah, interesting. I don't think it has to be required
There was a way to define a DbSet in a readonly way, but I can't remember what it looked like
s
let me see what happens if I remove the required part
d
I just found the relevant code snippet:
Copy code
csharp
public class MyContext : DbContext
{
    public DbSet<Customer> Customers => Set<Customer>();
}
If you make your DbSet like that, you shouldn't have any issues
s
I am slowely trying to learn the Umbraco way as I have to move a module (now developed in DNN Framework so aspnet 4.7) to Umbraco in near future. A lot is going very smoothly but some things are steap learning
after 15 years and more doing plain asp.net in the .net framework it gets me out of my comfort zone and I love it I must say
🙂
d
😊 I understand, I see a lot of people who have long experience with .NET Framework and struggle with .NET Core. I myself am rather fresh, when I started, .NET Core 3 was already a thing. There are so many exciting things to discover in .NET 6
s
🙌 Done. To undo this action, use 'ef migrations remove' so Thank you
d
Heuy! 🎉
s
Yes but on the other hand a lot of things are really recognisable.
and DNN allready introduced DI for example
and the option to use .net standard to use ,net core libs and use those via DI in the framework
Migration is implemented now the notification handler and I should be good to go
So 1 more thank you! Just kicked off the Umbraco site and the table is created in the sql db so thanks
d
You are welcome 😊.
a
Hi @Sander (Site4All Netherlands) it would be great if you could write a blog post about how to set EF Core things to successfully create custom tables from the Umbraco RCL package or you could add some contribution to Umbraco documentation as a small tutorial. I first would appreciate it. Thank you.
s
I am still learning Umbraco 🙂 But when I am a few steps further I am more than willing to write something about it. But I must say I came this far because of the excellent help of others here!
But next to the core and umbraco way I find it sometimes hard to switch my way of thinking I used for many years. We work a lot with DNN as framework and there there is not as much seperation of backend and front end. Many backend modules are build as you would build any front end module. more seperation via security roles. So learning the Umbraco way and finding out that most just work in Umbraco and not from plugin perspective (what I am used to) is 80% of the work
187 Views