Copied to clipboard

Flag this post as spam?

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


  • Anders Brännmark 224 posts 275 karma points
    1 week ago
    Anders Brännmark
    0

    Entity Framework in Umbraco 12 and custom tables in a separate logic project

    Trying to wire up to use Entity Framework with Umbraco EFCoreContext in a separate logic project but can seam to getting working. Anyone done this before?

    Seen this but when having a separate project, that isnt running the "Umbraco"? https://docs.umbraco.com/umbraco-cms/tutorials/getting-started-with-entity-framework-core

  • Mark Jarvis 5 posts 116 karma points
    1 week ago
    Mark Jarvis
    0

    Hello Anders,

    I've been working recently on a similar project. I found this article by Jon Jones extremely helpful;

    https://www.jondjones.com/learn-umbraco-cms/umbraco-12-tutorials/how-to/custom-database-tables-with-umbraco-12-and-entity-framework-deep-dive/

    The biggest issue I ran into was the using statement for IEFCoreScope in the controller.

    Instead of this type of declaration:
    using IEfCoreScope<MyContext> scope = _efCoreScopeProvider.CreateScope();
    
    I had to use the traditional type:
    using IEfCoreScope<MyContext> scope = _efCoreScopeProvider.CreateScope() {
     //Do DbContext things
    }
    

    Aside from that I setup a traditional Api to grab third party data and import it to the database.

    If you have any specific questions, I'd be glad help also, since it is fresh in my mind.

    Have a great day, --Mark

  • Anders Brännmark 224 posts 275 karma points
    1 week ago
    Anders Brännmark
    0

    Hi,

    Did you run this in a separate project from the "umbraco" web project?

    Can seam to wire up the dbcontext in the separate project any other way than with EF standard way.

    builder.Services.AddDbContext<CommerceContext>(options => options.UseSqlServer(_config.GetConnectionString("umbracoDbDSN"));
    //builder.Services.AddUmbracoEFCoreContext<CommerceContext>(_config.GetConnectionString("umbracoDbDSN"), _config.GetConnectionStringProviderName("umbracoDbDSN"));
    

    As the AddUmbracoEFCoreContext needs AddUmbraco before and that seams wrong in this case to add Umbraco with all whistles and bells? Or maybe it should add a slimed down version of the AddUmbraco statement? Anyone have a "core" AddUmbraco setup that one is supposed to use instead?

  • Anders Brännmark 224 posts 275 karma points
    1 week ago
    Anders Brännmark
    101

    Got it working so that migrations can be in the logic project now. Had to make a custom IDesignTimeDbContextFactory in the logic project that ef migrations use.

    public class CommerceDesignTimeContextFactory : IDesignTimeDbContextFactory<CommerceContext>
    {
        public CommerceContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<CommerceContext>();
            optionsBuilder.UseSqlServer();
            return new CommerceContext(optionsBuilder.Options);
        }
    }
    

    Wire up as recommended in the web project that references the separate project.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddUmbraco(_env, _config)
            .AddBackOffice()
            .AddWebsite()
            .AddDeliveryApi()
            .AddComposers()
            .Build();
        services.AddUmbracoEFCoreContext<CommerceContext>(_config.GetConnectionString("umbracoDbDSN"), _config.GetConnectionStringProviderName("umbracoDbDSN"));
    }
    
  • Mark Jarvis 5 posts 116 karma points
    1 week ago
    Mark Jarvis
    0

    Great! Glad you got it working. Yes that is a very similar approach that I took.

    I've built my migrations by hand and installed any required umbraco packages in the logic project to satisfy dependencies. As a temporary measure for testing I setup a pre-build script that copies AppCode/* and AppPlugins/* to the web project for execution and testing. Eventually I will roll that logic project into a nuget package and install it into my main site.

    --Mark

Please Sign in or register to post replies

Write your reply to:

Draft