0

I'm having a little trouble returning a table column name dynamically. I created an ASP net core 3.1 project in DDD standard and I am using Entity Framework Core v5.0.12.

And I created the entity as follows:

public class TBSISTEMAXXX
{
   public Int64? SISTEMAXXX_NS { get; set; }

   public string SISTEMAXXX_NM { get; set; }
   public string SISTEMAXXX_DS { get; set; }
   public string SISTEMAXXX_VIEW { get; set; }
   public string SISTEMAXXX_CONTROLLER { get; set; }
   public DateTime SISTEMAXXX_DT_CAD { get; set; }
   public DateTime? SISTEMAXXX_DT_INA { get; set; }
}

Where I have a RepositoryBase class, which holds all the "generic" functions, for example the insert, where I go through the entity looking for the "column" with the name "_DT_CAD" which would be the registration date of the record at the time of insert.

public async Task<T> InsertAsync(T Objeto)
{
   try
   {
      using (var data = new Context(_OptionsBuilder))
      {
         await data.Set<T>().AddAsync(Objeto);

         foreach (var entry in data.ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty(entry.Entity.GetType().Name + "_DT_CAD") != null))
         {
            if (entry.State == EntityState.Added)
            {
               entry.Property(entry.Entity.GetType().Name + "_DT_CAD").CurrentValue = DateTime.Now;
            }
         }

         await data.SaveChangesAsync();

         data.Entry(Objeto).GetDatabaseValues();
      }
   }
   catch (Exception)
   {
      throw;
   }

   return Objeto;
}

Before this function was working in the older version of Entity Framework Core, but now it is no longer working.

I've already researched some ways to fix this, but I still haven't had success if anyone.

To temporarily work around this, I left the code as follows:

public class TBSISTEMAXXX
{
   public Int64? SISTEMAXXX_NS { get; set; }

   public string SISTEMAXXX_NM { get; set; }
   public string SISTEMAXXX_DS { get; set; }
   public string SISTEMAXXX_VIEW { get; set; }
   public string SISTEMAXXX_CONTROLLER { get; set; }
   public DateTime SISTEMAXXX_DT_CAD { get; set; }  = DateTime.Now;
   public DateTime? SISTEMAXXX_DT_INA { get; set; }
}

However, I am having difficulties when making changes to the registry, as it always ends up being populated with the modification date and losing the date the registry was inserted.

If someone can help me, as I would like to keep the project with the most "recent" version and be able to get the name of the columns of the tables dynamically, as I will need to do this in other functions.

4
  • The DDD way of doing this would be to not let the repository set any kind of property on the objects (having public setters in general is not DDD style) but rather have a private constructor and a public static factory method which sets SISTEMAXXX_DT_CAD to DateTime.Now when the entity is created. This ensured that the date when the entity was created cannot be changed afterwards and also takes resposibility that the repository should not have to the entity. Commented Dec 16, 2021 at 11:36
  • Please clarify "not working". Even though, there is no way the shown code to find a property, since you are seeking for "{Type.Name}{PropertyName}`, while your class name is "TBSISTEMAXXX" and property names start with "SISTEMAXXX" (w/o "TB" prefix). Thus, this cannot have "worked" in any EFC version. Commented Dec 16, 2021 at 13:41
  • @Nannanas I understand, before I had a similar method in the context class that did this for me, but it stopped doing it when I started using entity with newer versions. Commented Dec 18, 2021 at 12:48
  • @Nannanas I left it below as I was using it in the context class, but as it is a function that every entity will use, the way I managed to do it was leaving it setting the date at the time of insert Commented Dec 18, 2021 at 12:53

1 Answer 1

0

It was like this in the context class:

 public override int SaveChanges()
        {

            foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty(entry.Entity.ToString().Substring(entry.Entity.ToString().Length - 12) + "_DT_CAD") != null))
            {
                if (entry.State == EntityState.Added)
                {
                    entry.Property(entry.Entity.ToString().Substring(entry.Entity.ToString().Length - 12) + "_DT_CAD").CurrentValue = DateTime.Now;
                }

                if (entry.State == EntityState.Modified)
                {
                    entry.Property(entry.Entity.ToString().Substring(entry.Entity.ToString().Length - 12) + "_DT_CAD").IsModified = false;
                }
            }
   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.