Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object already exists with Entity Framework Core initial migration

I'm in the process of learning how to use EF and I'm attempting to do an initial create migration using EF core. When I run Update-Database after running Add-Migration InitialCreate I receive an error:

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

The other SO questions concerning this issue typically had the person delete their db and do an initial migration to fix this issue. Because this is my initial create I'm unsure how to proceed.

I have no DB with a customers object, so I'm unsure why EF is telling me that there is already an object named 'Customers' when trying to create my new DB.

Now although I get this error it appears my tables are created for the new DB but I don't know what the cause of this error is.

I've deleted the new DB and and the Migrations folder and tried running the same commands as before in the console.

Add-Migration InitialCreate Update-Database

I still receive the same error. Below are my models, context and startup. Any insight into why I'm receiving this error would be much appreciated.

Customer Model

 public class Customer
 {
    [Key]
    [DatabaseGenerated( DatabaseGeneratedOption.Identity )]
    public int CustomerId { get; set; }

    [Required]
    [MaxLength( 50 )]
    public string FirstName { get; set; }

    [MaxLength( 50 )]
    public string LastName { get; set; }

    [Required]
    [EmailAddress]
    [MaxLength( 254 )]
    public string Email { get; set; }

    [Required]
    [Phone]
    [MaxLength( 15 )]
    public string PhoneNumber { get; set; }

    public ICollection<LodgingDates> LodgingDates { get; set; } =
        new List<LodgingDates>( );

    [Required]
    public CreditCard CreditCard { get; set; }

    [Required]
    [MaxLength( 100 )]
    public string StreetAddress { get; set; }

    [Required]
    [MaxLength( 60 )]
    public string City { get; set; }

    [Required]
    [MaxLength( 9 )]
    public string PostCode { get; set; }

    [Required]
    [MaxLength( 55 )]
    public string Country { get; set; }

    [Required]
    [MaxLength( 50 )]
    public string StateOrProvince { get; set; }

    [Required]
    public bool ReceiveEmailNotifications { get; set; }
 }

CreditCard Model

public class CreditCard
{
    [Key]
    [DatabaseGenerated( DatabaseGeneratedOption.Identity )]
    public int CreditCardId { get; set; }

    [Required]
    [MaxLength( 19 )]
    [CreditCard]
    public string AccountNumber { get; set; }

    [Required]
    [MaxLength( 4 )]
    public int Ccv { get; set; }

    [Required]
    public DateTime ExpirationDate { get; set; }

    [Required]
    [MaxLength( 100 )]
    public string StreetAddress { get; set; }

    [Required]
    [MaxLength( 60 )]
    public string City { get; set; }

    [Required]
    [MaxLength( 9 )]
    public string PostCode { get; set; }

    [Required]
    [MaxLength( 55 )]
    public string Country { get; set; }

    [Required]
    [MaxLength( 50 )]
    public string StateOrProvince { get; set; }

    public Customer Customer { get; set; }

    public int CustomerId { get; set; }
}

LodgingDates Model

public class LodgingDates
{
    [Key]
    [DatabaseGenerated( DatabaseGeneratedOption.Identity )]
    public int LodgingDatesId { get; set; }

    [Required]
    public DateTime CheckInDate { get; set; }

    [Required]
    public DateTime CheckOutDate { get; set; }


    public Customer Customer { get; set; }

    public int CustomerId { get; set; } 
}

Context

public sealed class LodgingCrmContext : DbContext
{
    public LodgingCrmContext( DbContextOptions<LodgingCrmContext> options ) 
        : base( options )
    {
    }

    public DbSet<Customer> Customers { get; set; } 
    public DbSet<CreditCard> CreditCards { get; set; }
    public DbSet<LodgingDates> LodgingDates { get; set; } 
}

Startup

public void ConfigureServices( IServiceCollection services )
    {
        services.AddDbContext<LodgingCrmContext>( options =>
            options.UseSqlServer( Configuration.GetConnectionString( "DefaultConnection" ) ) );
    }
like image 576
WBuck Avatar asked Mar 07 '23 22:03

WBuck


1 Answers

OK, I figured it out.

I restarted VS and navigated over to the LodgingCrmContext and noticed that the constructor had Database.EnsureCreated( ); in it. This was something I had removed earlier today. But when I restarted VS there it was in the ctor again (weird).

Once I removed Database.EnsureCreated( ); and rebuilt, my initial create migration worked.

The Database.EnsureCreated( ); was causing the Customers table to be created twice. This would also explain why the DB was being created (with the correct tables) and why I was seeing:

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

like image 181
WBuck Avatar answered Mar 25 '23 16:03

WBuck