Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7627875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:31:09+00:00 2026-05-31T05:31:09+00:00

We have an audit table in our database, and on update the old and

  • 0

We have an audit table in our database, and on update the old and new values are serialized to XML and stored in the same row. The objects are currently deep-cloned thus:

public EntityObject CloneEntity(EntityObject obj)
{
    DataContractSerializer dcSer = new DataContractSerializer(obj.GetType());

    MemoryStream memoryStream = new MemoryStream();

    dcSer.WriteObject(memoryStream, obj);

    memoryStream.Position = 0;

    EntityObject newObject = (EntityObject)dcSer.ReadObject(memoryStream);

    return newObject;
}

Whilst this works, it generates vast amounts of data due to the related records pulled from the deep clone, with hundreds of thousands of reads from the DB on dcSer.WriteObject(memoryStream, obj), and an eventual MemoryStream size of some 200MB, not to mention the amount of data being written back to the DB. Not ideal.

So I would like to do a memberwise clone instead, as it is my understanding that a memberwise clone would leave the object references out, and avoid copying all the related Entity Framework models.

So I did this:

public EntityObject CloneEntity(EntityObject obj)
{
    EntityObjectAuditable auditable = (EntityObjectAuditable)obj; // invalid cast exception

    return auditable.ShallowCopy();
}

// ....

public class EntityObjectAuditable : EntityObject
{
    public EntityObjectAuditable ShallowCopy()
    {
        return (EntityObjectAuditable)this.MemberwiseClone();
    }
}

but I get an invalid cast exception because the actual type of the incoming EntityObject is a subclass relating to the table itself.

I have also tried using an extension method to access MemberwiseClone(), but extension methods cannot access protected methods.

So, how can I create a shallow copy of a generic EntityObject?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:31:10+00:00Added an answer on May 31, 2026 at 5:31 am

    My first recommendation would be to try this, which is similar to what you’re doing now, but has worked for me with very little overhead:

    DataContractSerializationUtils.SerializeToXmlString(Entity, throwExceptions);
    

    Also, I’ve used this method before with success, and don’t find the output too verbose. It seems to be nearly identical to what you’re doing now.

        /// <summary>
        /// Creates an exact duplicate of the entity provided
        /// </summary>
        /// <param name="source">The source copy of the entity</param>
        /// <returns>An exact duplicate entity</returns>
        public TEntity Clone(TEntity Source)
        {
            // Don’t serialize a null object, simply return the default for that object
            if (ReferenceEquals(Source, null))
            {
                return default(TEntity);
            }
            var dcs = new DataContractSerializer(typeof (TEntity));
            using (Stream stream = new MemoryStream())
            {
                dcs.WriteObject(stream, Source);
                stream.Seek(0, SeekOrigin.Begin);
                return (TEntity) dcs.ReadObject(stream);
            }
        }
    

    If neither of these options seem appealing, my recommendation is to write a short function that uses reflection to copy any properties from the source entity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have some data stored in an audit table, where triggers on the
We have a simple table (an audit log) that our (3rd-party) product fills with
I have an audit table in my SQL Server 2008 database which contains the
I have an audit record table that I am writing to. I am connecting
I have been asked to audit any/all changes in a MySQL table. Does anyone
I have an audit table in SQL server. It is to record an entry
I have a table that has a Trigger associated with it for Update, Insert
We are planning on introducing simple Audit Trail in our database using triggers and
currently we have quite a chunky auditing system for objects within our application, the
Is there a way to update a record in an audit table (via triggers)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.