0

I have a SQL statement in C# in a string variable:

string sqlString = "UPDATE \"MyTable\""
                   + $" SET \"STATUS\" = {(int)StatusValue.Confirmed}"
                   + $" WHERE \"ID\" = {MessageId}";

But now I need to transform it to Entity Framework, but unfortunately I do not know Entity Framework well enough. How do I do that?

2 Answers 2

1

Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.

it would be like

 using (var db = new YourContextDB())
 {
  var result = db.TableName.SingleOrDefault(b => b.ColumnName == 
  parameter);
  if (result != null)
  {
    result.UpdatebleColumnName = "Some new value";
    db.SaveChanges();
  }
 }

check https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Sign up to request clarification or add additional context in comments.

2 Comments

You could use the actual column names, making some assumptions about the model.
yes, i could . But answered like this for his better understanding.
0

You don't need this string anymore. All you need is an instance of your DatabaseContext.

Your code would be similar to this one:

using var dbContext = new YourDbContext();
var message = dbContext.MyTable.FirstOrDefault(mt => mt.Id == MessageId);
message.Status = (int)StatusValue.Confirmed;
dbContext.SaveChanges();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.