Update Entity by Primary Key in SqlSugar

In order to update an entity based on the primary key, you can use the Updateable method in SqlSugar. Here is an example code:

var db = new SqlSugarClient(new ConnectionConfig
{
    ConnectionString = "YourConnectionString",
    DbType = DbType.SqlServer,
    IsAutoCloseConnection = true,
    InitKeyType = InitKeyType.Attribute
});

var entity = new YourEntity
{
    Id = 1,
    Name = "NewName"
};

var result = db.Updateable(entity).ExecuteCommand();

In this example, we created an entity object named ‘entity’ and set its Id and Name properties. Then, we passed this entity object to SqlSugar using the Updateable method and called the ExecuteCommand method to perform the update operation.

Please note that the parameter of the Updateable method can be either an entity object or a collection containing entity objects. If you need to update multiple entity objects, you can pass them in a collection to the Updateable method.

bannerAds