0

I have an application in ASP.NET Core-6 Web API using Entity Framework, and also utilizes code first data migration.

I did the migration this way:

Add-Migration InitialCreate -OutputDir Your\Directory

update-database –verbose

With this when I launched the application, it automatically created the tables.

The application is already running on live.

Models:

public abstract class AuditableEntity
{
    public Guid Id { get; set; }
    public DateTime CreatedAt { get; set; }

    public string CreatedBy { get; set; }

    public DateTime? LastModifiedAt { get; set; }

    public string LastModifiedBy { get; set; }

    [JsonIgnore]
    public bool? IsDeleted { get; set; }
    public string DeletedBy { get; set; }
    public DateTime? DeletedAt { get; set; }
}

public class MandateDetail : AuditableEntity
{
    public string ReferenceNumber { get; set; }
    public int? SerialNumber { get; set; }  
    public long PaymentReference { get; set; } 
    public string DrAccountNumber { get; set; }
    public string Narration { get; set; }
    public bool? IsNotified { get; set; }
    public decimal? PayableAmount { get; set; }
    public string PostingReference { get; set; }
    public string ProcessedBy { get; set; }
}

Configurations:

public class MandateDetailConfigurations : IEntityTypeConfiguration<MandateDetail>
{

    public void Configure(EntityTypeBuilder<MandateDetail> builder)
    {
        builder.ToTable(name: "mandate_details");
        builder.HasKey(m => new { m.PaymentReference, m.Id });
        builder.Property(m => m.Id).HasDefaultValueSql("NEWID()");
        builder.Property(m => m.PaymentReference).ValueGeneratedOnAdd();
        builder.Property(m => m.IsNotified).HasDefaultValue(false);
        builder.Property(m => m.PayableAmount).HasPrecision(20, 2);
        builder.Property(m => m.DueDate).HasColumnType("date");
        builder.Property(m => m.IsDeleted).HasDefaultValue(false);
        builder.Property(m => m.CreatedAt).HasColumnType("datetime");
        builder.Property(m => m.LastModifiedAt).HasColumnType("datetime");
        builder.Property(m => m.DeletedAt).HasColumnType("datetime");
        builder.HasOne<Mandate>(m => m.Mandate).WithMany(m => m.MandateDetails).HasForeignKey(m => m.MandateId);
    }
}

ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>,ApplicationUserRole, IdentityUserLogin<string>,IdentityRoleClaim<string>, IdentityUserToken<string>>
{
    private readonly ICurrentUserService _currentUserService;
    private readonly IDateTime _dateTime;
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
        ICurrentUserService currentUserService, IDateTime dateTime)
        : base(options)
    {
        _currentUserService = currentUserService;
        _dateTime = dateTime;
    }
    public DbSet<Mandate> Mandates { get; set; }
    public DbSet<MandateDetail> MandateDetails { get; set; }
    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
    public DbSet<ApplicationRole> ApplicationRoles { get; set; }
    public virtual DbSet<Permission> Permissions { get; set; }
    public virtual DbSet<RolePermission> RolePermissions { get; set; }
    public virtual DbSet<UserPermission> UserPermissions { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
        base.OnModelCreating(builder);

        builder.ApplyConfiguration(new ApplicationUserConfigurations());
        builder.ApplyConfiguration(new ApplicationUserRoleConfigurations());
        builder.ApplyConfiguration(new IdentityRoleClaimConfigurations());
        builder.ApplyConfiguration(new IdentityUserClaimConfigurations());
        builder.ApplyConfiguration(new IdentityUserLoginConfigurations());
        builder.ApplyConfiguration(new IdentityUserClaimConfigurations());
        builder.ApplyConfiguration(new ApplicationRoleConfigurations());
        builder.ApplyConfiguration(new IdentityUserTokenConfigurations());
        builder.ApplyConfiguration(new PermissionConfiguration());
        builder.ApplyConfiguration(new UserPermissionConfiguration());
        builder.ApplyConfiguration(new RolePermissionConfiguration());
        builder.ApplyConfiguration(new MandateConfigurations());
        builder.ApplyConfiguration(new MandateDetailConfigurations());
    }
}

However, I want to alter the table mandate_details (MandateDetail), add this field TnxStatusDesc VARCHAR(100) and make it nullable,update migration,

and it should be nullable.

Then when the application is launched, it should automatically update the database.

So, I added this to the Model (MandateDetail):

public string PaymentStatus { get; set; }

and updated MandateDetailConfigurations:

builder.Property(m => m.PaymentStatus).HasColumnType("varchar(100)");

In the Package Manager Console, I run:

Add-Migration AddPaymentStatusColumn -OutputDir Your\Directory

and finally did this:

Update-Database -verbose

But I got this error:

There is already an object named 'AspNetRoles' in the database.

When I added, -IgnoreChanges, it states that it's not recognised.

How do I resolve this?

NOTE: Kindly note that I don't have direct access to the database. Also the application is already being used.

Ayobamilaye
  • 1,099
  • 1
  • 17
  • 46
  • Please refer to this [link](https://stackoverflow.com/questions/43687433/update-database-command-is-not-working-in-asp-net-core-entity-framework-core-b/43687656#43687656). – Xinran Shen Aug 24 '23 at 07:36

0 Answers0