How to make optional required for differents attributes on differents inherited classes on EF6 , (businnes required rules)

asp.net-mvc-4 entity-framework entity-framework-6

Question

How to make optional REQUIRED for the same attribute on different inherited classes on EF6.

Why the 'required attribute' from one child is required for other child? Why does entity framework merge all data anotations to base classe 'Person' if the base class is non required attributes?

I've used the same classes on MVC to create required fields on cshtml, and it works. The MVC understands only required field from one child and not make any 'wrong merge' with those two child classes.

For example:

//EF Codefirst Class
public class Person
{
    [Key]
    public int key{get;set;}
    [StringLength(500)]
    public virtual string name { get; set; }
    [StringLength(500)]
    public virtual string email{ get; set; }
    [StringLength(500)]
    public virtual string phone{ get; set; }
    [StringLength(500)]
    public virtual string address{ get; set; }
    [StringLength(500)]
    public virtual string manager{ get; set; }
    [StringLength(500)]
    public virtual string Discriminator{ get; set; }
}
//My Inherited classes
public class Employee : Person
{
    [Required]
    public override string name{ get; set; }
    [Required]
    public override string phone{ get; set; }
    [Required]
    public override string manager{ get; set; }
} 
public class Manager: Person
{
    [Required]
    public override string name{ get; set; }
    [Required]
    public override string email{ get; set; }
}
//And my sample function 'Add PersonManager' 
 private void  InsertPerson()
 {
    using (var ctx = new MyDataContext())
    {
        try
        {
            var m = new Manager() ;
            m.name = "my name" ;
            m.email = "my@email.com";
            m.address =" something";

            ctx.Person.Add(m);        

            ctx.SaveChanges();  
        }
        catch (Exception ex)
        {
         // Why, if I try to Add my Person 'Manager', the attribute : phone and manager is REQUIRED?
        }        
    }
}
1
0
11/28/2017 7:04:23 PM

Accepted Answer

It happens because you are using TPH strategy. All entities will be merged in one table, EF handles what must be null or not null.

If you use TPT strategy, EF will create different tables for each entity. To learn more about inheritance strategies take a look at this link http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx

To use TPT instead of TPH, you must define a "key" in your child class, like this:

public class Employee : Person
{
    [Key]
    public int employeeId;
    [Required]
    public override string name;
    [Required]
    public override string phone;
    [Required]
    public override string manager;
} 

Another way to do this is using Fluent API. Like this:

modelBuilder.Entity<Person>() 
    .HasKey(c => c.key); 

modelBuilder.Entity<Employee>()
   .ToTable("Employees");

modelBuilder.Entity<Manager>()
   .ToTable("Managers");

To see more about this, take a look at this link https://msdn.microsoft.com/en-us/data/jj591617.aspx#2.5

1
7/30/2015 11:52:07 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow