If you want to be very specific, you can execute a raw command directly against the context like so;
using (var context = new BloggingContext())
{
context.Amodel.SqlQuery("UPDATE Amodels SET str2='com1' WHERE str1='val999'");
}
If it's required that you need to update on a non-key value, then you'd be looking to create a stored procedure that takes as parameters the values you want to update and what you want to update by. E.g. if you want to call on the context like so;
context.UpdateAModelByStr1(amodel.Str2, amodel.Str1);
Create a Stored Procedure in your DB such that;
CREATE PROCEDURE UpdateAModelByStr1(@Str2Value nvarchar(50),
@Str1UpdateBy nvarchar(50))
BEGIN
UPDATE AModels
SET Str2 = @Str2Value
WHERE Str1 = @Str1UpdateBy
END
To add the stored procedure to your EF context (make sure you've actually created the SP in the DB first), go to your entity data model (if you don't have one, right-click the project, Add -> New Item and select ADO.NET Entity Data Model. For a more detailed how-to, see here), right-click anywhere in the model and select Update Model From Database, tick the Stored Procedures and Functions box in the Add tab and select UpdateAModelByStr1. When you click Finish, a new method called UpdateAModelByStr1
is now added to your context as in the example above.
While using a stored procedure might seem like more than what you want to do, EF is not really designed to have the full feature set and flexibility of SQL itself, its purpose is to make the standard CRUD stuff much easier to get set up with with a few bells and whistles for common scenarios.