0

I've created an abstract class with some base properties:

public abstract class BaseModel
{
    public BaseWishModel()
    {
    }

    [Key]
    public int Id { get; set; }

    public virtual string Title { get; set; }

    public bool IsPublished { get; set; }
    public bool IsSpam { get; set; }
}

My item class:

public class PrivateItem : BaseModel
{
    [NotMapped]
    public string PurposesIds { get; set; }
}

My OnModelCreating method:

  modelBuilder.Entity<BaseModel>()
             .Map<PrivateItem>(r => r.Requires("Discriminator").HasValue((int)Enums.Type.Private))
             .ToTable("Items");

When I save the data it's generates next sql:

INSERT [dbo].[Items]([Title], [IsPublished], [ShortDescription1], [ShortDescription2], [Discriminator])

I don't know why it's generates ShortDescription1 and ShortDescription1

Arnold
  • 89
  • 1
  • 11
  • Yes I have. this is a mapping ` modelBuilder.Entity() .Map(r => r.Requires("Discriminator").HasValue((int)Enums.Type.Private)) .Map(r => r.Requires("Discriminator").HasValue((int)Enums.Type.Public)) .Map(r => r.Requires("Discriminator").HasValue((int)Enums.Type.Other)) .ToTable("Item"); ` I'm submitting only one insert with specific class. Why is does it happen? – Arnold Jul 23 '14 at 16:21
  • 1
    Stack Overflow allows you to update your question as many times as you want. Please update your question with relevant code so other who read your question won't have to read every comment to understand the full question (and allows the code to be formatted for easy reading). – Erik Philips Jul 23 '14 at 16:31

1 Answers1

0

As, according to your comment, you have other classes inheriting from BaseModel, and with no other configuration from you, EF uses TPH by default.

Basically this leads to a single table for all the classes hierarchy.

As all classes of the hierarchy are persisted in the same table when an insert, for one class, is done, all columns (of the hierarchy) are populated. The non used by the class columns are populated by null or default value.

This bring the ShortDescription1 and ShortDescription2 in your insert query.

tschmit007
  • 7,559
  • 2
  • 35
  • 43
  • I'm trying to point all my classes to the same table. according to entity framework guide I can create Discriminator to differentiate between classes mapped to same table – Arnold Jul 23 '14 at 16:47
  • I'm sorry for confusion, but I already created Discriminator property and still I get the error – Arnold Jul 23 '14 at 17:08
  • Can you give me an example for proper implementation of my request? – Arnold Jul 23 '14 at 21:25