Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • René 3 posts 83 karma points
    Nov 23, 2022 @ 18:05
    René
    0

    EF Core, DBContext and Umbraco 10

    I have run into a problem when i am using "Microsoft.EntityFrameworkCore.SqlServer"

    I add my DBContext like this to startup

    "services.AddDbContext<DataContext>(options => options.UseSqlServer(_config.GetConnectionString("umbracoDbDSN")));"
    

    Then umbraco DB connection fails and Umbraco returns to showing install page any thoughts on why ?

    I need to access custom tables with EF

  • Lotte Pitcher 49 posts 242 karma points MVP 7x c-trib
    Nov 24, 2022 @ 18:36
    Lotte Pitcher
    0

    Hi René,

    I know I'm not solving your actual problem but could you use a separate SQL database for EF? That was the approach I took and managed to get things wired up ok!

    Just a thought :)

    Lotte

  • René 3 posts 83 karma points
    Nov 25, 2022 @ 12:45
    René
    0

    Any ideas are welcome :)

    so yes i could use a setup where i just use another DB to store my custom tables in.

  • Lotte Pitcher 49 posts 242 karma points MVP 7x c-trib
    Nov 25, 2022 @ 17:05
    Lotte Pitcher
    0

    There are people who believe that using a separate database for non umbraco data is the safer / right approach to take anyway! At least then you know that Umbraco migrations and EF migrations would never have any conflict. Good luck :)

  • René 3 posts 83 karma points
    Nov 25, 2022 @ 17:31
    René
    0

    I actually thought you had a solution you wanted to share?, because I still have problems using the

    "Microsoft.EntityFrameworkCore.SqlServer"

    package, when I install that package it's a bit like there is a conflict and umbraco doesn't know how to use the UmbracoDBDSN connection string and goes directly to the install umbraco page.

    so do you have a running example where you connect to two DB´s ? if so can you tell me how you did it without this "conflict" using "Microsoft.EntityFrameworkCore.SqlServer"

  • Biagio Paruolo 1621 posts 1914 karma points c-trib
    Nov 26, 2022 @ 17:53
    Biagio Paruolo
    100

    Hi,

    you have to use a separate connection string and context for your EF and you have to use the same Umbraco package version of Microsoft.EntityFrameworkCore.SqlServer.

  • Karl Lee 4 posts 77 karma points
    Apr 24, 2023 @ 12:11
    Karl Lee
    0

    I just came across the same issue. I resolved by adding the following references in the project file, Item group:

    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
    

    The build status told me which packages to reference.

  • Richard Ockerby 39 posts 214 karma points MVP 4x c-trib
    1 week ago
    Richard Ockerby
    0

    I ran into this issue in 13.5.2 recently and none of the above worked for me. The problem was that I was following the Umbraco documentation for connecting for custom data which applies to data within the same database as the main Umbraco instance.

    There was a chat over in Discord that helped me out, basically instead of builder.Services.AddUmbracoDbContext you should use builder.Services.AddDbContext and then instead of using the IEFCoreScopeProvider use a TransactionScope, like so:

    builder.Services.AddDbContext<MyDbContext>((serviceProvider, options) =>
    {
        options.UseSqlServer(builder.Config.GetConnectionString("customDbDSN"));
    });
    

    Elsewhere in your code....

    public class CustomDataRepository(MyDbContext myDbContext)
    {
        public async Task<IEnumerable<SomeClass>> GetAll()
        {
            using (var scope = new TransactionScope())
            {
                var data = myDbContext.SomeClasses.ToList();
                scope.Complete();
                return data;
            }
        }
    }
    

    Here's the Discord chat for more reference.

Please Sign in or register to post replies

Write your reply to:

Draft