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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:47:38+00:00 2026-05-20T07:47:38+00:00

I have the following entity public class DocumentHistory { public string Name { get;

  • 0

I have the following entity

public class DocumentHistory
{
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime DateModified { get; set; }
    public User ModifiedbyUser { get; set; }
    public string HistoryAction { get; set; }

    public virtual int DocumentId { get; set; }
    public virtual Document Document { get; set; }
}

In my DbContext, I define the keys using:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // DocumentHistory properties
        modelBuilder.Entity<DocumentHistory>()
                    .HasKey(x => new { x.DocumentId, x.DateModified });   
    }

In my automated integration tests, I initialize one record with

_doc.HistoryRecords.Add(new DocumentHistory { DateModified = DateTime.Now });

I then attempt to create a new record via:

        // Create the history record
        var history = new DocumentHistory
        {
            Name = doc.Name,
            Description = doc.Description,
            DateModified = DateTime.Now,
            HistoryAction = ScrawlConstants.HistoryActions.Update,
            ModifiedbyUser = user
        };
        doc.HistoryRecords.Add(history);
        _context.SaveChanges();

When SaveChanges occurs, the following exception occurs:

System.Data.SqlServerCe.SqlCeException: A duplicate value cannot be inserted into a unique index. [ Table name = DocumentHistories,Constraint name = PK__DocumentHistories__000000000000006E ]

Theoretically, every record should be unique due to the difference in datetimes, but this isn’t the case. What am I doing wrong?

  • 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-20T07:47:39+00:00Added an answer on May 20, 2026 at 7:47 am

    The accuracy of the clock on most machines is not high enough to produce a different value every time you read it.

    Instead, on a typical system the accuracy is around 10-15ms. This means that consecutive reads of the clock within the same “window” will produce the exact same value.

    See the DateTime.Now documentation for some information on this subject. Different sources of documentation gives different numbers but the relevant information is that it just isn’t accurate enough. One source stipulated 1/64th of a second, which translates to 15.625ms.

    You should instead switch to something that is guaranteed to be unique, such as a Guid (though you lose the ordering property of the data) or use a server-generated identity key.

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

Sidebar

Related Questions

I have the following entity public class Employee { public virtual int Id {get;set;}
I have the following entity public class Employee { public int EmployeeID { get;
I have the following annotated Hibernate entity classes: @Entity public class Cat { @Column(name
I have the following entity structure: public class Party { public Int32 PartyId {
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have the following entity class (in Groovy): import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue
I have a problem with the following Linq query using Entity Framework: from o
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,

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.