Does anyone have a sample for registring dbcontext...
# package-development
s
Does anyone have a sample for registring dbcontext services.AddUmbracoEFCoreContext<> when nit having access to startup cs? So use composer? Anyone good sample?
s
Copy code
csharp
public class MyComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.Services.AddUmbracoEFCoreContext<BlogContext>("{YOUR CONNECTIONSTRING HERE}", "{YOUR PROVIDER NAME HERE}");
    }
}
should do the trick, add
using Umbraco.Extensions;
and I think it should just work!
j
You can also try to get IConfiguration to not have to hardcode your connection string if you use the umbraco db - we do this and it works just fine:
Copy code
csharp
var configuration = services.BuildServiceProvider().GetService<IConfiguration>();
var connectionString = configuration?.GetSection("ConnectionStrings").GetSection("umbracoDbDSN").Value;
var provider = configuration?.GetSection("ConnectionStrings").GetSection("umbracoDbDSN_ProviderName").Value;

if (connectionString is not null && provider is not null)
{
    services.AddUmbracoEFCoreContext<FavouriteListContext>(connectionString, provider);
    services.AddUmbracoEFCoreContext<ProductInFavouriteListContext>(connectionString, provider);
}
s
I've tried that exactly
(and yes if I get this to work I will use connection from configuration, thanx)
btw I use 12.1.2 atm
j
Hmm I am also using 12.1.2, and while we do have DI set up slightly differently as we have our migrations in a seperate project it seems like all I have is using Umbraco.Extensions and these packages installed for it to work:
s
You pointed me exactyly in the right direction
thank you
s
The magic of Rider that figures all of this out for me Sorry, had to plug Rider again, I feel like they owe me commission at this point 😛
s
🙂
I am on regular vs
Never used rider
s
Try it! 😉
n
Be aware we accidentally broke the database provider being added automatically in v12.2. so you will have to add UseSqlServer() or similar in that version. But I have a solution coming that could make it much more streamlined to get going with EF Core I just need to describe it and present it in the PR: https://github.com/umbraco/Umbraco-CMS/issues/14893
2 Views