Currently, I've written a repository in which entities are being stored. That entity is an object and is inheriting from the base class "Entity".
public class Entity
{
#region Constructors
public Entity() : this(DateTime.Now, DateTime.Now) { }
public Entity(DateTime creationDate, DateTime updateDate)
{
DateCreated = creationDate;
DateUpdated = updateDate;
}
#endregion
#region Properties
[Key]
[Required]
[Column(Order= 0)]
public int Id { get; private set; }
[Required]
[Column(Order = 998)]
public DateTime DateCreated { get; private set; }
[Required]
[Column(Order = 999)]
public DateTime DateUpdated { get; internal set; }
#endregion
}
Now I want to unit test (not integration test) the repository holding an object that implements an Entity.
I've created a fake repository that uses a HashSet to store objects in memory. Now, the problem is that, when I add entities to my repository, all the Id values are assigned '0' which is normal.
On Entity Framework, they are assigned a unique int value (starting from 0), which is also normal due to the data annotations.
Now I would like to know how I can mimic the same behaviour for the Data Annotations when running unit tests, because right now, I can have multiple records in my repository which the same 'Key' value, which should off course not be possible under any circumstances.
Thanks.