10

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.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
Ala
  • 111
  • 6
  • 3
    look [here](http://stackoverflow.com/questions/6553935/ef-code-first-invalid-column-name-discriminator-but-no-inheritance) :) – Jens Kloster Nov 20 '13 at 06:56
  • 4
    @JensKloster, that sample is not inheriting from any class. I've seen it. But another class inherits from it. – Ala Nov 20 '13 at 06:59
  • 1
    @JensKloster, answers in that question indeed have inheritance. `PersonViewModel` inherits from `Person`. – Saeed Neamati Nov 20 '13 at 07:08
  • I got carried away with the title - it happens :| – Jens Kloster Nov 20 '13 at 07:19
  • 1
    @JensKloster: I think the comment was right (it is related, but not the same). However the close vote (don't know who cast it) was bad. When casting a close vote one must be perfectly convinced that it is a duplicate because once closed, questions are seldom reopened. – Anders Abel Nov 20 '13 at 07:22
  • @JensKloster PS: You can retract your close vote. If it was a mistake. More info [here](http://meta.stackexchange.com/a/167514/133638) – Jehof Nov 20 '13 at 07:31
  • Thanks :) It *was* me who voted to close - I apologize - the vote is retracted – Jens Kloster Nov 20 '13 at 07:46

1 Answers1

4

Your connection string might differ from your context! Please check this article.

Entity Framework follows the Convention over Configuration school of thought. This means that even in finding the related connection string, Entity Framework, uses the name of your context class (in this case MiniAccounting) and tries to find a connection string with that name in the configuration file.

However, if it doesn't find anything, it's fallback to light version of SQL (I guess SQL CE, or SQL Express, not sure about it), creates a database by itself, and tries to insert into it.

In your case, check the configuration file, and see if it matches the name of your context class. If not, user this constructor:

public class MiniContext : DbContext
{
    public MiniContext()
         : base("YourConnectionStringNameHere")
    {
        Database.SetInitializer<MiniContext>(null);
    }

    public DbSet<PurchaseItem> PurchaseItems { get; set; }
}
  • I still don't understand why the error message talks about a 'Discriminator' column when there clearly is no such column in the entity. – Anders Abel Nov 20 '13 at 09:22
  • @AndersAbel I agree with u.but i copy and past the code and check the connection string then all thing was correct –  Nov 20 '13 at 10:08