Variable interfering with the value of another variable

0

Good afternoon Srs.

I'm facing a problem I've never seen before, below I have a view mapped in EF with fluent

var viewRelatorio = db.view.where(ra.DataBruta.Month == mesAno.Month).toList();

I have two dictionaries that will interact with the data of this view

var rel1 = new Dictionary<int, Relatorio>();
var relItemVirada = new Dictionary<int, Relatorio>();

var relatorio = new Relatorio>();
var eventoFinal = false

I read the view for iteration with the dictionaries

foreach (var item in viewRelatorio)
{
  rel1.Add(i, item);                                       
  if (item.HorarioInicial.StartsWith("23") &&  item.HorarioFinal.StartsWith("00"))
  {
    relatorio = item;
    relatorio.HorarioFinal = "235959";
    relItemVirada.Add(i, relatorio);        
  }
  i++;
}

What happens is that every time that I change the value in report. FinalTime it changes the original value in item that does not undergo any kind of change, would like to know what that may be?

I have tried to interact in different ways, different loops, directly in the item, whenever I move in an item affects the next or vice versa

follows the fluent mapping

public RelatorioConfiguration()
        : base("name=" + ConfigurationManager.AppSettings["databaseInstance"].ToString())
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var relatorio = modelBuilder.Entity<Relatorio>();

        relatorio.ToTable("view_Relatorio");

        relatorio.HasKey(p => new {p.DataBruta, p.HorarioInicial});
        relatorio.Property(p => p.Ano);
        relatorio.Property(p => p.Classificacao);
        relatorio.Property(p => p.DataBruta);
        relatorio.Property(p => p.HorarioFinal);
        relatorio.Property(p => p.HorarioInicial);
    }

    public DbSet<Relatorio> Relatorio { get; set; }
    
asked by anonymous 07.02.2017 / 18:57

1 answer

3

If I understand correctly, your problem is not in EF but rather because classes are by reference rather than by value in .Net.

I'll listen to this question of yours:

  

What happens is that every time I change the value in report.Time end it changes the original value into item that does not undergo any kind of change, would like to know what that can be?

Viewing your code relatorio = item; when you assign "item" to another variable "report" you do not "copy" the item but only a reference and so the two variables at the moment point to the same object. So if you change a "report" property, it will change into "item" as well. So this code relatorio.HorarioFinal = "235959" is also changing the value of "EndTime item".

For example I've made an example in DotNetFiddle link .

One solution is to perform a clone of the object and one of the ways to do this is by using the BinaryFormatter, an example code below:

    public static T DeepClone<T>(this T source) where T : class
    {
        using (Stream cloneStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();

            formatter.Serialize(cloneStream, source);
            cloneStream.Position = 0;
            T clone = (T)formatter.Deserialize(cloneStream);

            return clone;
        }
    }
}
    
07.02.2017 / 19:37