Ho una tabella SQL con colonna TimeStamp
. L'entità EF corrispondente è sotto
public partial class Request : IEntityBase
{
public Request()
{
}
public int RequestID { get; set; }
public Nullable<int> BatchID { get; set; }
public string ClientID { get; set; }
public Nullable<System.DateTime> CreatedDateTime { get; set; }
public Nullable<System.DateTime> ModifiedDateTime { get; set; }
public byte[] VersionStamp { get; set; }
}
La proprietà VersionStamp
ha datatype timestamp
è sql
In C # creo una nuova entità e chiamo savechanges ()
var request = new Request()
{
ClientID = "XYZ",
CreatedDateTime = DateTime.Now,
ModifiedDateTime = DateTime.Now,
};
await _DBContext.SaveChangesAsync().ConfigureAwait(false);
Ma ottengo errore
"Impossibile inserire un valore esplicito in una colonna timestamp. Utilizza INSERT con un elenco di colonne per escludere la colonna timestamp o inserisci un DEFAULT nella colonna timestamp."
Non sto impostando il timestamp
nessuna parte del codice. Mi aspettavo che SQL aggiornerà automaticamente il valore della colonna
Per EF Core 2.0 (e anche EF Core 1.0) è necessario specificare la proprietà timestamp / version all'interno di DbContext / OnModelCreating mehod.
Di seguito è possibile trovare le specifiche di esempio per il tipo "Richiesta".
modelBuilder.Entity<Request>(entity =>
{
entity.ToTable("Requests"); // Depends on your table name
entity.HasKey(e => e.RequestID) // If you have a primary key
.HasName("PK_Requests");
entity.Property(e => e.VersionStamp)
.IsConcurrencyToken() // If you want to handle concurrency
.ValueGeneratedOnAddOrUpdate(); // This is important
});