https://discord.umbraco.com logo
#package-development
Does anyone have a sample for registring
# package-development
s

Sander (Site4All Netherlands)

10/04/2023, 7:23 PM
Does anyone have a sample for registring dbcontext services.AddUmbracoEFCoreContext<> when nit having access to startup cs? So use composer? Anyone good sample?
s

Sebastiaan

10/05/2023, 11:56 AM
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

jespermayntzhusen

10/05/2023, 12:21 PM
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

Sander (Site4All Netherlands)

10/05/2023, 12:28 PM
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

jespermayntzhusen

10/05/2023, 12:36 PM
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: https://cdn.discordapp.com/attachments/1159209153781321738/1159469128264990832/image.png?ex=653122c6&is=651eadc6&hm=895b9f1a7efe8a02fb6c863b07d3f00307eb0ad974d26dd3901a0cff8425bdf1&
s

Sander (Site4All Netherlands)

10/05/2023, 2:01 PM
You pointed me exactyly in the right direction
thank you
s

Sebastiaan

10/05/2023, 2:20 PM
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

Sander (Site4All Netherlands)

10/05/2023, 2:32 PM
🙂
I am on regular vs
Never used rider
s

Sebastiaan

10/05/2023, 3:24 PM
Try it! 😉
n

nikcio

10/06/2023, 8:13 PM
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