0

My "ShoppingCart" and "ShoppingCartItems" tables are already in my database. I am trying to add a new table called "discountCodes". Each shoppingCart can have one or zero discountCodes.

The error I am receiving is: Invalid column name 'discountId'.

[Table("ShoppingCarts")]
    public class ShoppingCart
    {


        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        [Column("cartID")]
        public string cartID { get; set; }
        public virtual IList<ShoppingCartItem> CartItems { get; set; }
        [Column("dateCreated")]
        public DateTime? DateCreated { get; set; }
        [Column("userID")]
        public Guid UserID { get; set; }
        public int? discountId { get; set; }
        public virtual Discount discount { get; set; }
    }

    [Table("discountCodes")]
    public class Discount
    {

        public int discountId { get; set; }
        public string discountCode{get;set;}
        [Required]
        public int percentOff { get; set; }
        [Required]
        public Boolean isActive { get; set; }
        public  ShoppingCart ShoppingCart { get; set; }

    }
public class ShoppingCartContext : DbContext
{

    public ShoppingCartContext()
        : base("MYDBConnectionString")
    {
        Database.SetInitializer<ShoppingCartContext>(new CreateDatabaseIfNotExists<ShoppingCartContext>());

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ShoppingCart>().HasKey(t => t.cartID)
                     .HasOptional(t => t.discount)
                     .WithOptionalPrincipal(d => d.ShoppingCart)
                     .Map(t => t.MapKey("cartID")); 
        modelBuilder.Entity<Discount>().HasKey(t => t.discountId)
                    .HasOptional(q => q.ShoppingCart);
    }   
    public DbSet<Discount> discountCodes { get; set; }
    public DbSet<ShoppingCart> ShoppingCart { get; set; }
    public DbSet<ShoppingCartItem> ShoppingCartItems { get; set; }

}
user1698144
  • 754
  • 4
  • 13
  • 36
  • what is the name of your discountID column in your db? You didn't specify a column name, so it assumes the name is the same as the property. – Brino May 26 '15 at 20:50
  • Yes, discountIDd is the identity column. – user1698144 May 26 '15 at 20:53
  • So what do the database tables look like? And *when* do you get this error? – Gert Arnold May 26 '15 at 20:56
  • I am using code-first ef. The database is generated from those classes. – user1698144 May 26 '15 at 20:57
  • But when is the error received? After an attempted insert? During initialization? – jjj May 26 '15 at 20:58
  • Also, your `OnModelCreating` shows that both sides are optional. [This might be a useful read](http://stackoverflow.com/questions/21889367/is-it-possible-to-capture-a-0-1-to-0-1-relationship-in-entity-framework). – jjj May 26 '15 at 21:06
  • Finally, would it make sense for a `Discount` to be associated with multiple `ShoppingCart`s? In other words, using a many-to-many relationship, with some validation logic restricting the number of `someShoppingCart.Discounts` to zero or one. That seems easier to handle with EF – jjj May 26 '15 at 21:10

1 Answers1

0

If you are working on an existing database you have to implement a DbMigration like it's explain here: Code First Migrations.

If you are in development phase, the easiest way is to drop the database.

nlips
  • 1,258
  • 9
  • 25