Doubt about mapping in Entity Framework 6. "readonly" properties

Asked

Viewed 398 times

10

I have three tables in my bank:

  • TB_COLABORADOR
  • TB_JORNADA
  • TB_JORNADA_COLABORADOR

To TB_JORNADA_COLABORADOR makes the association of employees with the journey and vice versa through the fields COD_JORNADA and COD_COLABORADOR. It also has two other fields: DAT_FIM and DAT_INICIO:

Diagrama

My question is this: how to map so that I can bring the data of the collaborator and the journey in two fields that I added in my class (DadosColaborador and DadosJornada) without the Entity Framework trying to update them when I go to make a data persistence:

public partial class JornadaColaborador
{
    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 virtual Colaborador DadosColaborador { get; set; }
    public virtual Jornada DadosJornada { get; set; }
}

My mapping was done as follows (currently I am ignoring the persistence of DadosColaborador and DadosJornada, only that at the same time I can not bring their data when I need):

public class JornadaColaboradorMap : EntityTypeConfiguration<JornadaColaborador>
{
    public JornadaColaboradorMap()
    {
        //Tabela
        ToTable("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 am making 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);
    }
  • I don’t know if I can do this. My suggestion would be to load the association into a Viewmodel and treat punctually the modification you need in the Action who receives the POST. If you want me to put that in answer.

  • Thank you if you can put as an answer to understand better.

  • 1

    @Gypsy

  • It was bad. In the rush I left to answer "later" and ended up not responding. I will do this now.

1 answer

3


Since there is no native update guarantee mechanism only on the first level of data, the way is to make a Viewmodel and carry out manual updates.

An implicit operator can be a good option 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 if any update occurs "by accident".

In the Controller:

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

Don’t forget to change the View:

@model SeuSistema.ViewModels.JornadaColaboradorViewModel 

When saving, normally send seo Viewmodel to the 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 */
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.