Inheritance in entity framework by hierarchy

0

I have a problem that I can not solve days ago, I am working with enttity framework codefirst in vs2015 with c #, I have a hierarchy, summarizing:

Person -IDPersona -first name -last name

User -IDPersona -user -password

Operator -IDPersona -level -cantHoras

Now, with codefirst when creating entities I do not create an inheritance, what I believe is that a User has "inside" a person, does not make the Abstract Person class.

How should I define the model? the Heritage? the classes to then do an Add-migration and an update-database and the inheritance works

Thank you very much

    
asked by Lucho 02.02.2017 в 13:52
source

2 answers

0

If you use inheritance in your classes EF creates you the tables linked by FK's, that is, you would have:

class Persona {
 // String porque asumo que es seguridad social o algo de eso
 public string IdPersona { get; set;}
 public string Nombre { get; set;}
 public string Apellidos { get; set;}
}

class Usuario : Persona {
 public string Password { get; set;}
}

class Operario : Usuario {
 public TimeSpan HorasTrabajadas { get; set;}
}

So when you create the Operario you create a Usuario and a Persona each in your table.

    
answered by 02.02.2017 в 14:52
0

Thank you very much for answering, yes, indeed, I did it as a decision, but now I have another problem, that the Automapper gives me an error:

Possibly unhandled rejection: {"data":{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent'1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Error getting value from 'Personas' on 'System.Data.Entity.DynamicProxies.Pais_99B12FE4944F620F290A1CD03520808445E30572E841FE2B695341C90D9FA9BE'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":"   en Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n   en Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues

Being a country that is proper to the person, that is the parent class If I do this, everything works fine: .ForMember (dst = > dst.Pais, opt = > opt.Ignore ()) But it is not the idea to ignore the country, since I need to have it ... Thanks

    
answered by 03.02.2017 в 14:46