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 an attribute that is optional REQUIRED for different inherited classes using EF6.

Why does the other child need the "needed characteristic" from the first child? When the base class "Person" has no needed properties, why does entity framework merge all data annotations to it?

I created needed fields in cshtml using the same classes I used in MVC, and it works. The MVC does not perform any "bad merges" with those two child classes and only recognizes the needed fields from one child.

For instance:

//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

ZZZ_tmp
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