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 7556145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:47:40+00:00 2026-05-30T11:47:40+00:00

Lets assume the following code handling the SaveChanges event of a DataContext void Context_SavingChanges(object

  • 0

Lets assume the following code handling the SaveChanges event of a DataContext

void Context_SavingChanges(object sender, EventArgs e)
    {

        IEnumerable<ObjectStateEntry> objectStateEntries =
            from ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted)
            where ose.Entity != null
            select ose;

        foreach (ObjectStateEntry entry in objectStateEntries)
        {
            foreach (var field in entry.CurrentValues.DataRecordInfo.FieldMetadata)
            {
                var guid = Guid.NewGuid();
                AuditEntry audit = AuditEntry.CreateAuditEntry(
                    id: guid,
                    entitySet: entry.EntitySet.Name,
                    typeName: entry.Entity.GetType().Name,
                    entityId: (entry.CurrentValues["Id"] as string) ?? ((entry.State == EntityState.Added) ? "New" : null),
                    oldValue: (entry.State != EntityState.Added) ? entry.OriginalValues[field.FieldType.Name].ToString() : "New",
                    newValue: (entry.State != EntityState.Deleted) ? entry.CurrentValues[field.FieldType.Name].ToString() : "Deleted",
                    modifier: Environment.UserDomainName + "\\" + Environment.UserName,
                    dbAction: Enum.GetName(typeof(EntityState), entry.State),
                    field: field.FieldType.Name,
                    dateStamp: DateTime.Now
                );


                this.AuditEntries.AddObject(audit);
            }
        }
    }

When I create a new Entity and attempt to save it’s changes I get this error:

Cannot insert the value NULL into column ‘Id’, table ‘TimeEF.dbo.AuditEntries’; column does not allow nulls. INSERT fails.
The statement has been terminated.

When as you can see I have set the Id, any ideas? Bug?

  • 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-30T11:47:41+00:00Added an answer on May 30, 2026 at 11:47 am

    Have you examined the id column of your object in the EDMX file? If its StoreGeneratedPattern attribute is marked as “computed” or “identity”, then EF will not carry the C# value over to its SQL statement. Set the StoreGeneratedPattern attribute to “None” and the SQL that Entity Framework writes will contain your C#-created value for id.

    <EntityType Name="AuditEntry">
        //...
        <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="None" />
        //...
    </EntityType>
    

    For more information, see http://msdn.microsoft.com/en-us/library/dd296755(v=vs.90).aspx.

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

Sidebar

Related Questions

Lets assume we have the following code: abstract class Base1 { protected int num;
Let's assume I have the following code: public class MainClass { public static void
Lets assume the following code within a controller: $this->view->addScriptPath('dir1/views/scripts'); $this->view->addScriptPath('dir2/views/scripts'); $this->render('index.phtml'); Where dir1/views/scripts contains
When writing C++, let's assume the following line of code: Object* obj = new
Lets assume I have the following Java code: public String foo() { // returns
ok, this should be interesting. lets assume i have the following code: in this
Lets assume I have the following : 1- a code snippet CODE1 with time
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View

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.