Click here to Skip to main content
15,887,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Am trying to create a database from existing migration persistence database
  public CMSDBContext(DbContextOptions<CMSDBContext> options)
            : base(options)
        {
        }
//Dbsets
   protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("dbo");
            base.OnModelCreating(modelBuilder);
            CustomDateConverter.DateToUTC(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

        }
        public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
        {
            DateTime currentDateTime = DateTime.UtcNow;
            foreach (var auditableEntity in ChangeTracker.Entries<IAuditable>())
            {
                if (auditableEntity.State == EntityState.Added || auditableEntity.State == EntityState.Modified)
                {
                    
                    switch (auditableEntity.State)
                    {
                        case EntityState.Added:
                            auditableEntity.Entity.CreatedAt = currentDateTime;
                            break;
                        case EntityState.Modified:
                            auditableEntity.Entity.UpdatedAt = currentDateTime;
                            break;
                    }
                }
            }

            return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
        }

//Registering Services

public static IServiceCollection AddPresistenceInfrastructure(this IServiceCollection services)
        {
            services.AddDbContext<CMSDBContext>(options => options.UseSqlServer(SettingManager.CMSConnectionString));

            #region Repositories
            services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
            services.AddTransient(typeof(IReadonlyRepository<>), typeof(ReadonlyRepository<>));
            services.AddTransient(typeof(ISectionRepository), typeof(SectionRepository));
            #endregion

            #region Cache
            var redisConfigSection = SettingManager.GetSection(RedisSettings.SectionName);

            services.AddSingleton<IConnectionMultiplexer>(sp => ConnectionMultiplexer.Connect(new ConfigurationOptions
            {
                EndPoints = { $"{redisConfigSection["Server"]}:{redisConfigSection["Port"]}" },
                AllowAdmin = true,
                AbortOnConnectFail = false,
            }));

            services.AddSingleton<ICacheService, RedisCacheService>();
            #endregion
            return services;
        }
    }


appsettings.json
"CMSConnection": "Server=Sandy-NB\\LOCALSERVER;Database=cms_db;User Id=sa;Password=xxxx;"


there are multiple migration files and no seed

What I have tried:

I got these error
PM> Update-database --verbose
Build started...
Build succeeded.
Unable to create an object of type 'CMSDBContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728


There were solutions about making the project as startup project (already done and no success)

I tried to initially create an empty database with the database name and tried again -- no success

Pretty sure that the code has no issues because it was working previously on other machines but what would be the possible issue causing this problem?
Posted
Updated 12-Aug-23 23:36pm

1 solution

Please make sure from the starting host : the api project is selected
from the package manager console, make sure the persistance project is selected
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900