Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Database values reverted to their defaults when updating the database

$
0
0

Ecommerce web site, written using ASP.NET Core 3.1, Razor pages, EF 3.1.8 code first.

I have a model called Global. When I first set this up, I included the default values for four properties (others not shown for clarity)...

[Column(TypeName = "decimal(18,2)")]
public decimal DeliveryCharge { get; set; } = 3.5M;

[Column(TypeName = "decimal(18,2)")]
public decimal FreeDeliveryOver { get; set; } = 70.0M;

public string StripePublicKey { get; set; } = "pk_test_...";
public string StripeSecretKey { get; set; } = "sk_test_...";

When I deployed the site to the production server, I manually set the two Stripe keys to the live values, so the site could take payment.

This all worked fine until one day, seemingly out of the blue, we got an exception from Stripe, as the site was using the test keys again. I checked the Globals table, and yes, the test keys were back in there.

It's happened since, both on the production server and on my local machine. I'm not 100% sure, but I think the problem occurs after updating the database.

The odd thing is that on the first couple of times this happened, the two decimal properties in there did not have their values reset to the defaults. I know this, as the FreeDeliveryOver one was changed to 50.0M shortly after we launched, and that didn't get reverted when the issue was first noticed. However, the issue just happened again, and this time that property was set to zero (not the original default value), but DeliveryCharge was left at 3.50M (the default), so it seems to be inconsistent as to which values are reverted. The two Stripe keys always are, the FreeDeliveryOver wasn't the first couple of times, but was just now, and DeliveryCharge has never been reverted.

What I don't understand is why Entity Framework is changing these values. I would have thought that they would only have been used when the table was first created, or when a property was added. In the latest change, the Globals table wasn't touched, so I can't see why EF changed the data in it.

To clarify...

  • We don't have a constructor on the Global model
  • The only place the Stripe keys ever appeared in the code base was in the initialisers shown above, and in the migrations that EF created.
  • Whilst the initialisers were still there, this issue would cause the Stripe key properties to be set to the test values. Since removing the initialisers, the two keys are now set to null.
  • The Globals table only has one row (I know, we really should be using the app settings file for this!), and we never update the Stripe keys in the code, we only ever read them. The two decimals shown above can be updated from an admin page, but they haven't done this, certainly not just as I was updating the database, so that's not the cause.

Anyone any idea what's going on? I'm really nervous about this, as I don't trust EF any more. How do I know its not reverting values in other tables?

Thanks for any help you can give.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>