.NET Development
Cloning a record from one table into another without hard coding column names (using EF 5.0) Similar to what CopyDataRow does
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;
}
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 ---