Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Code first migration not working when deploying on DXP/DXC

Vote:
 

Hi,

I am using foundation for a new project we have, I am facing an issue where the table is not getting created via EF Code first approach. It works when I do the update-database via the NuGet console on local. But after deploying to Integration on DXC it did not work. There was a similar issue earlier on this forum, I am doing all that was suggested on that topic, but the issue still persists. Below is all the code related to it:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Foundation.DataLayer.Entities
{
    [Table("Retailers")]
    public class Retailer
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        public string ImageName { get; set; }
        public string Url { get; set; }

    }
}
using Foundation.DataLayer.Entities;
using System.Data.Entity;

namespace Foundation.DataLayer
{
    internal class TestDataContext : DbContext
    {
        public TestDataContext() : base("EcfSqlConnection")
        {

        }
        public DbSet<Retailer> RetailerEntities { get; set; }
    }
}

I then ran the Enable-Migrations commad, and then Add-Migration TestTable. This then creates a Migration folder along with the 2 classes below:

namespace Foundation.Migrations
{
    using Foundation.DataLayer;
    using Foundation.DataLayer.Entities;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<TestDataContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(TestDataContext context)
        {
            var obj = new Retailer()
            {
                ImageName = "Test.jpg",
                Name = "Test Name",
                Url = "http://www.google.com"
            };
            context.RetailerEntities.AddOrUpdate(obj);
            context.SaveChanges();
        }
    }
}
namespace Foundation.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class TestTable : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Retailers",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                        ImageName = c.String(),
                        Url = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.Retailers");
        }
    }
}

I then added a new Initialization for this:

using System.Data.Entity;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using Foundation.DataLayer;


namespace Foundation.Infrastructure
{
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class MigrationInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<TestDataContext, Migrations.Configuration>());
        }

        public void Uninitialize(InitializationEngine context)
        {
            //Add uninitialization logic
        }
    }
}

I also get an error that Entity Framework 6.4 does not have the migration files, upon looking into the package I did not find any migration files under the tools folder in the EntityFramework.6.4.0 nuget package  and hence went down to 6.2.

Also, while I was debugging on Inte; I see that the seed method is not being triggered. On local the moment I we enter the update-database on the Nuget Console, it triggers the seed method.

The update schema app settings config is set to true, the connection string name is correct amd the custom initialization module has a dependency on EPiServer.Web.InitializationModule. Since I am using foundation and have not made any changes to the configurations, my expectation was this should be a smooth sail. But it doesnt seem to be the case, is there any configuration I need to turn on/off. 

#222223
Edited, Apr 30, 2020 15:31
Vote:
 

Are you still having issues with this?  Seems to be the same problem in a similar thread.  Can you turn on debug logging or entity framework, should be able to see some logging on while it is failing on Integration

#222410
May 05, 2020 15:00
Vote:
 

Any update regarding this issue ?

#246205
Dec 30, 2020 17:10
Vote:
 

I assume when you deploy from INT to Preproduction then database is not updating. While deploying from PaaS portal are you checking "Use maintenance page"? I notice database schema updated when you check this checkbox. Probably worth trying. 

#247975
Feb 01, 2021 22:24
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.