I have three tables in my bank:
- TB_COLABORATOR
- TB_JORNADA
- TB_JORNADA_COLABORADOR
TB_JORNADA_COLABORADOR
associates employees with the day and vice versa through the COD_JORNADA
and COD_COLABORADOR
fields. It also has two other fields: DAT_FIM
and DAT_INICIO
:
Myquestionisthis:howtomapsothatIcanbringemployeeandworkdataintotwofieldsIaddedinmyclass(DadosColaborador
andDadosJornada
)withouttheEntityFrameworktryupdatingthemwhenIdoadatapersistence:
publicpartialclassJornadaColaborador{publicintCodJornadaColaborador{get;set;}publicintCodJornada{get;set;}publicintCodColaborador{get;set;}publicDateTimeDataInicio{get;set;}publicNullable<DateTime>DataFim{get;set;}publicvirtualColaboradorDadosColaborador{get;set;}publicvirtualJornadaDadosJornada{get;set;}}
Mymappingwasdoneasfollows(I'mcurrentlyignoringthepersistenceofDadosColaborador
andDadosJornada
,butatthesametimeIcannotgetdatafromthemwhenIneedit):
publicclassJornadaColaboradorMap:EntityTypeConfiguration<JornadaColaborador>{publicJornadaColaboradorMap(){//TabelaToTable("TB_JORNADA_COLABORADOR");
//Atributos
HasKey(t => new { t.CodJornadaColaborador });
Property(t => t.CodJornadaColaborador).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("COD_JORNADA_COLABORADOR");
Property(t => t.CodJornada).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed).HasColumnName("COD_JORNADA");
Property(t => t.CodColaborador).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed).HasColumnName("COD_COLABORADOR");
Property(t => t.DataInicio).HasColumnName("DAT_INICIO");
Property(t => t.DataFim).HasColumnName("DAT_FIM");
Ignore(x => x.DadosColaborador);
Ignore(x => x.DadosJornada);
}
}
NOTE: I'm doing data persistence using generic methods, examples:
public void Atualizar(T obj)
{
ctx.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
ctx.Entry<T>(obj).State = EntityState.Modified;
}
public void Adicionar(T obj)
{
ctx.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
ctx.Set<T>().Add(obj);
}