0

I have a table Master

  • Id
  • Tag
  • Text
  • Discriminator

Data in it is like

    <table>
        <tr>
            <td>Id</td>
            <td>Tag</td>
            <td>Text</td>
            <td>Discriminartor</td>
        </tr>
        <tr>
            <td>1</td>
            <td>20</td>
            <td>"test1"</td>
            <td>Field20</td>
        </tr>
        <tr>
            <td>2</td>
            <td>21</td>
            <td>"test1"</td>
            <td>Field21</td>
        </tr>
        <tr>
            <td>3</td>
            <td>22</td>
            <td>"test1"</td>
            <td>Field22</td>
        </tr>
    </table>

In my model class I have class

 public class Master
{    public int Id{ get; set; }     
     public string Tag{ get; set;} 
     public string Text { get; set; }
}

and I have Classes for Field20, Field21 and Field22 as below

    public class Field20 : Master
{     public Field20()
    {
        Tag= "20";
        Text= "Sender's Reference";
    }
}
   public class Field21 : Master
{     public Field21()
    {
        Tag= "21";
        Text= "Sender's Reference";
    }
}
   public class Field22 : Master
{     public Field22()
    {
        Tag= "22";
        Text= "Sender's Reference";
    }
}

In Mapping I have

modelBuilder.Configurations.Add(new MasterMap());
   modelBuilder.Entity<Master>()
            .Map<Field20>(p =>   p.Requires("Discriminator").HasValue("Field20"))
         .Map<Field21>(p => p.Requires("Discriminator").HasValue("Field21"))
        .Map<Field22>(p => p.Requires("Discriminator").HasValue("Field22"))

Now when i am trying to add value to master like

          var fields = new Master(){Tag= "22", Text = "123.1"}
          _context.Master.Add(fields)
         _context.savechanges()

I am getting

Cannot insert the value NULL into column 'Discriminator', table 'Master'; column does not allow nulls. INSERT fails. The statement has been terminated.

user3182464
  • 53
  • 1
  • 8

1 Answers1

0

This line...

var fields = new Master(){Tag= "22", Text = "123.1"};

...creates an object for which no discriminator is defined, because it's not a subtype. You should create a subtype instead:

var field = new Field20(){Tag= "22", Text = "123.1"};

To help you not create the wrong objects, make Master an abstract class.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Thanks for the reply but I know it will work that way. In my scenario I don't know which Field I have to save. It could be either of Field20, 21 or 22. So I want something like the way i mentioned above. – user3182464 Mar 08 '16 at 21:36
  • Then I wouldn't implement this as inheritance. Simply set a type field in code and act on its value. There is no other way to create a subtype than explicitly creating it in some typed way. Often, inheritance is not the best pattern anyway. There are other behavioral patterns based on composition rather than inheritance (this is an old debate). – Gert Arnold Mar 08 '16 at 21:43
  • The only alternative is to create the appropriate subtype in a `switch` statement and `Add` it to the `Master` DbSet. – Gert Arnold Mar 16 '16 at 12:51