Sto usando SQL Server 2016 e Entity Framework Core.
Diciamo che vogliamo usare una stringa concatenata in una sequenza come chiave primaria di una tabella.
Ho creato la tabella utilizzando la migrazione Code-First e ho impostato il valore predefinito della colonna come concat desiderato (Screenshot da SSMS):
Quando inserisco manualmente le righe nella tabella utilizzando il comando INSERT, funziona come previsto. Se scelgo di omettere il valore della colonna, viene generato dalla sequenza, altrimenti - utilizza il valore fornito.
Tuttavia, quando provo a inserire entità per codice, viene generata un'eccezione
System.InvalidOperationException: 'Impossibile creare o tracciare un'entità di tipo' EntityName 'perché ha un valore di chiave primaria o alternata null.'
Il campo pertinente dell'entità è una semplice proprietà stringa. Ho provato ad aggiungere l'annotazione [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
, ma ciò non ha aiutato.
EDIT: quando chiamo Add (Sync)
traccia dello stack -
Result StackTrace:
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableKeyIdentityMap`1.Add(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func`2 handleNode)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityStates(IEnumerable`1 entities, EntityState entityState)
Quando chiamo traccia dello stack AddAsync
-
Result StackTrace:
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableKeyIdentityMap`1.Add(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func`2 handleNode)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityStates(IEnumerable`1 entities, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.AddRangeAsync(IEnumerable`1 entities, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.AddRangeAsync(IEnumerable`1 entities, CancellationToken cancellationToken)
at (Rest of the stack trace...)
Eventuali suggerimenti? Grazie
Puoi provare a impostare il valore StoreGeneratedPattern su Identity. Sto usando l'approccio Database First e l'impostazione di questa proprietà mi ha aiutato molto.