简体   繁体   中英

Entity Framework Core get entity column name

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.

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;
                }
            }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM