I want to test a very simple Code-First example. I have a class called PurchaseItem
which is not inherited from any based class and also no other class inherits from it. It has no association with other models at all:
public class PurchaseItem
{
public int Id { get; set; }
public string Buyer { get; set; }
public string Item { get; set; }
public int Quantity { get; set; }
public int Price { get; set; }
}
Here's my database context code. I set the database initializer to null
, because I already have database:
public class MiniContext : DbContext
{
public MiniContext()
{
Database.SetInitializer<MiniContext>(null);
}
public DbSet<PurchaseItem> PurchaseItems { get; set; }
}
And here's my code to save a record:
public void SavePurchaseItems(string buyer, string item, int quantity, int price)
{
PurchaseItem purchaseItem = new PurchaseItem
{
Buyer = buyer,
Item = item,
Quantity = quantity,
Price = price
};
using (MiniContext context = new MiniContext())
{
context.PurchaseItems.Add(purchaseItem);
context.SaveChanges();
}
}
No inheritance. No inheritance at all. Just a simple, stand-alone table. But what I get is:
Invalid column name 'Discriminator'.
On context.SaveChanges();
line. What's wrong? What should I do?
Update: I've already seen the EF Code First “Invalid column name 'Discriminator'” but no inheritance question. That question hasn't solved my problem. In that question, the OP do has inheritance in his code.