0

I got this error when I try to add a new "parameter" object into database, I have:

  • "parameter" abstract class
  • "parameter_manual_input_pattern" class inherits "parameter" abstract class
  • "parameter_store_pattern"class inherits "parameter" abstract class
  • "Store" class

Note that the class inherit only properties which is related to pattern.

[Table("parameter")]
public abstract class parameter
{
    [Key]
    public string Parameter_Id { get; set; }
    public string Pattern { get; set; }
    
    public virtual string x { get; set; }
    public virtual string y { get; set; }
    

    [ForeignKey("Store")]
    public virtual string Store_Id { get; set; }
    public virtual Store Store { get; set; }
}

public class parameter_manuel_input_pattern
{
    public override string x { get; set; }
}

public class parameter_store_pattern
{
    public override string y { get; set; }

    [ForeignKey("Store")]
    public override string Store_Id { get; set; }
    public override Store Store { get; set; }
}


//DbContext
public class MyContext : DbContext
{
    public MyContext() : base()
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<parameter>()
        .Map<parameter_store_pattern>(m => m.Requires("Pattern").HasValue("store"))
        .Map<parameter_manual_input_pattern>(m => m.Requires("Pattern").HasValue("manual"));
     }
}
  • Why do you have all these `virtual` properties in the base class and `override`s in the derived? Remove them from the base class and just *define* them in the appropriate derived classes. – Ivan Stoev Jul 20 '17 at 11:48
  • I defined like that when i would like to instanciate an object with type of derived class : Parameter param = new parameter_store_pattern(); param.X = "test"; – Abdelatif Latrach Jul 20 '17 at 12:06
  • If i don't define the propety "x" in base class, how to access it within derived class ? – Abdelatif Latrach Jul 20 '17 at 12:08
  • The point was you shouldn't be doing it that way. Although TPH will consolidate all the fields into a single table, you still have to define them at the appropriate class. For instance, you should define the property `x` in the `parameter_manuel_input_pattern` class - `public string x { get; set; }` (after removing it from the base class). – Ivan Stoev Jul 20 '17 at 12:10
  • if i remove the property "x" from base class and definied into derived class and to instanciate then an object as: baseClass param = new derivedClass(), I cannot access the property of derived class and this is a problem – Abdelatif Latrach Jul 20 '17 at 12:25
  • any help please? – Abdelatif Latrach Jul 20 '17 at 12:50

0 Answers0