Doubt about mapping in the Entity Framework 6. Properties "readonly"

9

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(DadosColaboradorandDadosJornada)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'mcurrentlyignoringthepersistenceofDadosColaboradorandDadosJornada,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);
    }
    
asked by anonymous 15.06.2015 / 15:33

1 answer

3

Since there is no native update-only mechanism at the first data level, the approach is to do a ViewModel and perform the updates manually.

An implicit operator may be a good choice here:

public class JornadaColaboradorViewModel
{
    public int CodJornadaColaborador { get; set; }
    public int CodJornada { get; set; }
    public int CodColaborador { get; set; }

    public DateTime DataInicio { get; set; }
    public Nullable<DateTime> DataFim { get; set; }

    public Colaborador DadosColaborador { get; set; }
    public Jornada DadosJornada { get; set; }

    public static implicit operator JornadaColaboradorViewModel(JornadaColaborador jornadaColaborador) 
    {
        return new JornadaColaboradorViewModel 
        {
            CodJornadaColaborador = jornadaColaborador.CodJornadaColaborador,
            CodJornada = jornadaColaborador.CodJornada,
            CodColaborador = jornadaColaborador.CodColaborador,
            DataInicio = jornadaColaborador.DataInicio,
            DataFim = jornadaColaborador.DataFim,
            DadosColaborador = jornadaColaborador.DadosColaborador.
            DadosJornada = jornadaColaborador.DadosJornada
        };
    }
}

Notice that I took the virtual on purpose. Without it, the Entity Framework does not identify that it is an association, in case some update occurs "by accident".

Controller :

var jornadaColaborador = contexto.JornadaColaboradores.SingleOrDefault(/* Coloque algum critério de seleção aqui */);
var jornadaColaboradorViewModel = (JornadaColaboradorViewModel)jornadaColaborador;
return View(jornadaColaboradorViewModel);

Do not forget to change the View :

@model SeuSistema.ViewModels.JornadaColaboradorViewModel 

When saving, normally send ViewModel to Controller :

[HttpPost]
public ActionResult Salvar(JornadaColaboradorViewModel viewModel) 
{
    var jornadaColaboradorOriginal = contexto.JornadaColaboradores.Single(j => j.CodJornadaColaborador = viewModel.CodJornadaColaborador);
    jornadaColaboradorOriginal.CodJornada = viewModel.CodJornada;
    jornadaColaboradorOriginal.CodColaborador = viewModel.CodColaborador;
    jornadaColaboradorOriginal.DataInicio = viewModel.DataInicio;
    jornadaColaboradorOriginal.DataFim = viewModel.DataFim;

    /* Faça o resto da lógica de negócio aqui */
}
    
25.06.2015 / 16:25