I am inserting a new row to the database table using Entity Framework, but my problem is that column ValidFromDate is causing an exception
The property ‘ValidFromDate’ is part of the object’s key information
and cannot be modified date
Our DBA has defined database and image below is the snapshot of the EDMX file. ValidFromDate is a datetime column.
Idea of the FarmAnimal is to track the history of where certain animal has been. Therefore AnimalId and ValidFromDate causes the row to be unique.
Now the question is that how can I can insert new row to the table that has this kind of schema?

Insert using Entity Framework
var farmAnimal = new FarmAnimal {
AnimalId = insert.AnimalId,
ValidFromDate = insert.ValidFromDate // exception comes from this line,
etc.
};
entities.FarmAnimals.Add(farmAnimal);
Update: StackTrace
at System.Data.Objects.EntityEntry.DetectChangesInProperties(Boolean detectOnlyComplexProperties)
at System.Data.Objects.ObjectStateManager.DetectChangesInScalarAndComplexProperties(IList`1 entries)
at System.Data.Objects.ObjectStateManager.DetectChanges()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at xxx.Repositories.PairingRepository.UpdateFarmAnimals(IEnumerable`1 updates, IEnumerable`1 inserts) in xxx
at xxx.Services.PairingService.RemovePairingAnimals(List`1 animalIds) in xxx
at xxx.Controllers.PairingController.RemovePairingAnimals(List`1 animalIds) in xxx
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
Update 2: Entity Framework files
I have added three files Entity Framework mapping files / generators
EDMX file
ADO.NET DbContext Generator
ADO.NET EntityObject Generator (POCO’s)
The DbSet wrapper fails before it even does anything with your new FarmAnimal – DetectChanges is called before AddObject on the underlying ObjectContext, and it somehow detects forbidden changes in a FarmAnimal already loaded/added to the context. If you’re using POCOs with proxies or just plain boring classes generated with change tracking (that’s NOT the same as “Self-Tracking Entities”, just the classes that get generated when you add a new data model with vanilla VS2010), you can turn off automatic detection of changes so that it does not get called.