0

I created models and performed code first migration which resulted in prepopulated up down methods. However at a later stage I added new models to my application. The new models I added were Cart, OrderDetails and Order. I then typed add-migration for each of these models which as a result produced empty up down methods.

I would like to ask why are these up down methods empty when I added a new model. I referenced these models in the dbcontext, the same dbcontext that referenced previously created models.

These are the new model classes that I added:

public class OrderDetail
{
    public int OrderDetailId { get; set; }
    public int OrderId { get; set; }
    public int BookId { get; set; }

    public int Quantity { get; set; }

    public decimal UnitPrice { get; set; }

    public virtual Book Books { get; set; }
    public virtual Order Order { get; set; }

}


public class Cart
{
        [Key]
        public int RecordId { get; set; }
        public string CartId { get; set; }
        public int BookId{ get; set; }
        public int Count { get; set; }
        public virtual Book Book { get; set; }
}

class Order
{
    public int OrderId { get; set; }

    public string Username { get; set; }

    public string FirstName { get; set; }
}

Here is my dbcontext

public BookStoreOnlineDB() : base("name=BookStoreOnlineDB")
    {
    }

    public System.Data.Entity.DbSet<BookStoreOnline.Models.Book> Books { get; set; }

    public System.Data.Entity.DbSet<BookStoreOnline.Models.Author> Authors { get; set; }

    public System.Data.Entity.DbSet<BookStoreOnline.Models.BookStatus> BookStatus { get; set; }

    public System.Data.Entity.DbSet<BookStoreOnline.Models.Genre> Genres { get; set; }

    public System.Data.Entity.DbSet<BookStoreOnline.Models.Order> Orders { get; set; }

    public System.Data.Entity.DbSet<BookStoreOnline.Models.OrderDetail> OrderDetails { get; set; }

         public System.Data.Entity.DbSet<BookStoreOnline.Models.Cart>Carts {  get; set; }
}

In summary, how do I populate the new up down methods with regards to the newly added Cart, OrderDetail and Detail models.

N.B. The orderDetails model and cart model reference the book model(book model contains had data migration performed on it at an earlier stage and contains populated up down methods).

New to this and would really appreciate help. Thanks

willwolfram18
  • 1,747
  • 13
  • 25
Sam
  • 149
  • 2
  • 15
  • Try running Update-Database -Script -SourceMigration $InitialDatabase. Does it generate a script with your prior objects? If you are not too far along just [reset your migrations](https://weblog.west-wind.com/posts/2016/jan/13/resetting-entity-framework-migrations-to-a-clean-slate) – Steve Greene Nov 10 '16 at 18:29
  • @SteveGreene Did not work but thanks anyways – Sam Nov 13 '16 at 21:52
  • 1
    Your question says add-migration had no code. Your solution says add-migration now has code. What did you do different or change? – Steve Greene Nov 14 '16 at 17:07
  • I typed add-migration intitialcreate as appose to add migration orderdetails – Sam Nov 14 '16 at 19:59
  • 1
    Assuming you meant "add-migration orderdetails", then it would make no difference. "initialcreate" is just a user defined label and does not affect creation of the migration itself. – Steve Greene Nov 14 '16 at 20:04

1 Answers1

-1

Answer: in PM Console: add-migration initialcreate //this included the newly added models e.g their ids,(cart,orderdetails, order models) to the up down methods

Sam
  • 149
  • 2
  • 15