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

  • Home
  • SEARCH
  • 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 3497836
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:25:09+00:00 2026-05-18T12:25:09+00:00

I have several tables in my database that have read-only fields that get set

  • 0

I have several tables in my database that have read-only fields that get set on Inserting and Updating, namely: AddDate (DateTime), AddUserName (string), LastModDate (DateTime), LastModUserName (string).

All of the tables that have these values have been set to inherit from the following interface:

public interface IUserTrackTable
{
    string AddUserName { get; set; }
    DateTime AddDate { get; set; }
    string LastModUserName { get; set; }
    DateTime LastModDate { get; set; }
}

As such, I have the following method on the Edit.aspx page:

protected void DetailsDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
{
    IUserTrackTable newObject = e.NewObject as IUserTrackTable;
    if (newObject != null)
    {
        newObject.LastModUserName = User.Identity.Name;
        newObject.LastModDate = DateTime.Now;
    }
}

However, by the time it hits this method, the e.OriginalObject has already lost the values for all four fields, so a ChangeConflictException gets thrown during the actual Update. I have tried adding the four column names to the DetailsView1.DataKeyNames array in the Init event handler:

protected void Page_Init(object sender, EventArgs e)
{
    // other things happen before this
    var readOnlyColumns = table.Columns.Where(c => c.Attributes.SingleOrDefaultOfType<ReadOnlyAttribute>(ReadOnlyAttribute.Default).IsReadOnly).Select(c => c.Name);
    DetailsView1.DataKeyNames = DetailsView1.DataKeyNames.Union<string>(readOnlyColumns).ToArray<string>();

    DetailsView1.RowsGenerator = new CustomFieldGenerator(table, PageTemplates.Edit, false);
    // other things happen after this
}

I’ve tried making that code only happen on PostBack, and still nothing. I’m at a lose for how to get the values for all of the columns to make the round-trip.

The only thing the CustomFieldGenerator is handling the ReadOnlyAttribute, following the details on C# Bits.

UPDATE: After further investigation, the values make the round trip to the DetailsView_ItemUpdating event. All of the values are present in the e.OldValues dictionary. However, they are lost by the time it gets to the LinqDataSource_Updating event.

Obviously, there are the “solutions” of making those columns not participate in Concurrency Checks or other ways that involve hard-coding, but the ideal solution would dynamically add the appropriate information where needed so that this stays as a Dynamic solution.

  • 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-18T12:25:09+00:00Added an answer on May 18, 2026 at 12:25 pm

    i Drovani, I assume you want data auditing (see Steve Sheldon’s A Method to Handle Audit Fields in LINQ to SQL), I would do this in the model in EF4 you can do it like this:

    partial void OnContextCreated()
    {
        // Register the handler for the SavingChanges event. 
        this.SavingChanges += new EventHandler(context_SavingChanges);
    }
    
    private static void context_SavingChanges(object sender, EventArgs e)
    {
        // handle auditing
        AuditingHelperUtility.ProcessAuditFields(objects.GetObjectStateEntries(EntityState.Added));
        AuditingHelperUtility.ProcessAuditFields(objects.GetObjectStateEntries(EntityState.Modified), InsertMode: false);
    }
    
    internal static class AuditingHelperUtility
    {
        internal static void ProcessAuditFields(IEnumerable<Object> list, bool InsertMode = true)
        {
            foreach (var item in list)
            {
                IAuditable entity = item as IAuditable;
                if (entity != null)
                {
                    if (InsertMode)
                    {
                        entity.InsertedBy = GetUserId();
                        entity.InsertedOn = DateTime.Now;
                    }
    
                    entity.UpdatedBy = GetUserId();
                    entity.UpdatedOn = DateTime.Now;
                }
            }
        }
    }
    

    Sadly this is not possible with EF v1

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

Sidebar

Related Questions

I have several tables whose only unique data is a uniqueidentifier (a Guid) column.
I have created a Python module that creates and populates several SQLite tables. Now,
I have a long running insert transaction that inserts data into several related tables.
I have a SQL Mobile database with one table. It has several columns with
This issue is driving me mad. I have several tables defined, and CRUD stored
I have several sources of tables with personal data, like this: SOURCE 1 ID,
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on
For several reasons that I don't have the liberty to talk about, we are
I have an HTML table with several columns and I need to implement a
While trying to use LINQ to SQL I encountered several problems. I have table

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.