I'm running a very simple database entity update using EntityFrameworkCore.SqlServer v 5.0.2 but I'm getting a weird behavior... On my local environment the update query is working just fine, but when I switch the environment to Production I get an exception.
I debugged further and realized that in production EntityFramework is trying to insert a record in a different table using the same primary key ID which is resulting in the exception.
Here's my code snippet:
var package = await _context.Packages.FindAsync(request.Id);
if (package == null) throw new RestException(HttpStatusCode.NotFound, new { package = "Could not find Package" });
package.Name = request.Name ?? package.Name;
package.ValidUntil = request.ValidUntil ?? package.ValidUntil;
package.Variants = request.Variants ?? package.Variants;
package.Active = request.Active ?? package.Active;
var success = await _context.SaveChangesAsync() > 0;
Some notes: Variants is an entity that includes an Enum called CurrencyId. I have a database table the keeps the values of those enums as records in the Db
public class Variant
{
public Guid Id { get; set; }
public string Name { get; set; }
public SubscriptionTypeEnum SubscriptionTypeId { get; set; }
public virtual SubscriptionType SubscriptionType { get; set; }
[Column(TypeName = "decimal(18,2)")]
public decimal VariantPrice { get; set; }
public CurrencyEnum CurrencyId { get; set; }
public virtual Currency Currency { get; set; }
}
and the exception that is thrown (only in production) is the following
Microsoft.Data.SqlClient.SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_Currencies'. Cannot insert duplicate key in object 'dbo.Currencies'. The duplicate key value is (0).
The statement has been terminated.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__169_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
ClientConnectionId:a1e3bf81-e486-4e22-9ce6-e0b6601d4bd1
Error Number:2627,State:1,Class:14
Again to re-iterate this is only happening in production but locally the update is working fine.
In clue what might be the issue and how to fix it?
User contributions licensed under CC BY-SA 3.0