简体   繁体   中英

When making simple UPDATE, I'm getting '@P0' inserted into column instead of value I'm passing in

In my asp.net core / EF Core app, if a Customer table stripe_customerID field is null, I need to generate a new string value and update the customer record. I'm using the code below to try and do this.

Customer = _dbcontext.Customer.FirstOrDefault(m => m.fk_Email == User.Identity.Name);
if (string.IsNullOrEmpty(Customer.stripe_CustomerID))
{
   string NewStripeCustomerID = "abc123";
   var result = _dbcontext.Database
   //.ExecuteSqlInterpolated($"EXECUTE UpdateStripeCustomerID @StripeCustomerID='{NewStripeCustomerID}', @CustomerID={Customer.CustomerID}");
   .ExecuteSqlInterpolated($"UPDATE Customer SET stripe_CustomerID='{NewStripeCustomerID}' WHERE CustomerID={Customer.CustomerID}");

When I run this using either the direct UPDATE sql raw command, or using the commented out stored procedure, both set the field to the value '@p0'

stripe_CustomerID
@p0

To make matters more confusing, when I pause the debugger on the line, and copy the interpolated string, I get:

$"EXECUTE UpdateStripeCustomerID @StripeCustomerID=N'{NewStripeCustomerID}', @CustomerID={Customer.CustomerID}" = "EXECUTE UpdateStripeCustomerID @StripeCustomerID='abc123', @CustomerID=1"

Which looks exactly right, and if I execute it in MSSSMS, it of course works fine.

I'm at a loss why @p0 is being inserted.

 .ExecuteSqlInterpolated($"UPDATE Customer SET stripe_CustomerID = '{NewStripeCustomerID}' WHERE CustomerID = {Customer.CustomerID}");

Single quote ( ' ) is not needed for {NewStripeCustomerID} .

Try this:

 .ExecuteSqlInterpolated($"UPDATE Customer SET stripe_CustomerID = {NewStripeCustomerID} WHERE CustomerID = {Customer.CustomerID}");

Please also share stored procedure code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM