.NET Development
Cloning a record from one table into another without hard coding column names (using EF 5.0) Similar to what CopyDataRow does
Gravatar is a globally recognized avatar based on your email address. Cloning a record from one table into another without hard coding column names (using EF 5.0) Similar to what CopyDataRow does
  Alejandro A Sosa
  All
  Nov 30, 2021 @ 06:09pm

Hi Rick,

In CopyDataRow of Westwind Utilities you copy the content of one DataRow to another, one column at a time, while skipping errors.

I am using EF Core 5.0 and want to clone records from one table to another without having to hard code the column names. Is there a way to do this with EF Core 5.0 in a similar way that you do in CopyDataRow, perhaps converting the entity objects into DataRows?

Thank you very much,

Alex

        public static bool CopyDataRow(DataRow source, DataRow target)
        {
            DataColumnCollection columns = target.Table.Columns;

            for (int x = 0; x < columns.Count; x++)
            {
                string fieldname = columns[x].ColumnName;

                try
                {
                    target[x] = source[fieldname];
                }
                catch {; }  // skip any errors
            }

            return true;
        }
Gravatar is a globally recognized avatar based on your email address. re: Cloning a record from one table into another without hard coding column names (using EF 5.0) Similar to what CopyDataRow does
  Rick Strahl
  Alejandro A Sosa
  Dec 1, 2021 @ 03:27pm

Nope...

I already told you what you have to do which is using reflection to copy object data.

If you really need to just 'copy' data as a whole, just detach it from the context, change the change state of the object, re-attach it to the context and save it. There's no need to copy anything.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Cloning a record from one table into another without hard coding column names (using EF 5.0) Similar to what CopyDataRow does
  Alejandro A Sosa
  Rick Strahl
  Dec 6, 2021 @ 09:41am

Thanks for the answer

© 1996-2024