0

My app performs Authentication with Asp.NET Identity. The users logging in are stored in dbo.ASPNetUsers

A while ago, I used migrations to add another table called "Customer", but now the table is populated.

How can I add a controller to perform CRUD operations ONLY on existing "Customer"(the other tables have controllers) without dropping the table and risking erasing any of its content or any other content in the existing tables in the db.

What I tried so far:

Create class in Models folder called "Customer" to resemble the table columns

       public class Customer
       {
            [Key]
            public string ID {get;set;}

            public string FirstName {get;set;}

            public string LastName  {get;set;}
       }

Add DbSetCustomers

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
           // : base("DefaultConnection", throwIfV1Schema: false)
              : base("MS_TableConnectionString", throwIfV1Schema: false)
        {
        }

        public DbSet<Customer> Customers { get; set; }


        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

Add new Controller of type Web API 2 Controller with actions using Entity Framework using model class Customer, with data context class as the default ApplicationDBContext (default option when creating controller, im assuming it uses my app connection string for the azure table).

When using the GET customers in POST, I get an error saying the database has changed since last migration, I Imagine this is because adding the DbSet Customers but I do not want to update migration because in the migration code, there is Drop Table command.

  1. Is this right method?
  2. Should I just change the migration script not to drop the table?

Please advise.

1 Answer 1

1

"Database has changed since last migration" , it s a way of EF telling that your migration history doesn't match with the tables.

EF's default database generation workflow creates a full script that will recreate your database every time you select Generate Database from Model, so if you execute it in your DB you will lose all your data. However, if you just create a new Entity and did not change the existing ones, then you can still generate database from your Model but then take that script and only run the part that creates the new table for your new entity.

You can take a look at the below link for further reference:

https://www.apress.com/gp/blog/all-blog-posts/secular-trends-for-the-cloud/12097630

https://social.msdn.microsoft.com/Forums/en-US/ed3ccb7e-5e89-4fd4-ae92-d641a5c5bd9a/entity-framework-model-first-make-changes-to-the-database-without-dropping-tables?forum=adodotnetentityframework

https://www.pauric.blog/Database-Updates-and-Migrations-with-Entity-Framework/

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.