首页 文章

从Entity Framework更新SQL位列

提问于
浏览
0

我在使用C#和Entity Framework更新SQL数据库中的“位”列时遇到问题 .

我有一个'Settings'表,其中一个NOT NULL位列名为'Active' . 当我在表中创建记录并在代码中将“Active”指定为“true”或“false”时,创建的数据库中的记录是准确的,“Active”列包含在代码中指定的正确值 . 当我更新记录并在代码中将“Active”从“false”更改为“true”时,也可以 . 但是,当我将记录从“true”更新为“false”时,数据库中的“Active”列仍包含“1”(true) .

这是一个已知的问题?如果是这样,有解决方法吗?我做了大量的研究,却找不到任何东西 .

这是我的更新代码:

public int UpdateSetting(SettingModel settingModel)
{
    using (var db = new EfMyDB())
    {
        // Create a new setting record with the ID of the record to update.
        Setting updatedSetting = new Setting { Id = settingModel.Id };

        // Attach the record.
        db.Settings.Attach(updatedSetting);

        // Update the attached record.
        updatedSetting.Name = settingModel.Name;
        updatedSetting.Value = settingModel.Value;
        updatedSetting.Active= settingModel.Active;

        // Save the database changes.
        return db.SaveChanges();
    }
}

EF'活动'列属性:

Properties in the EDMX file for the 'Active' column.

Versions :.NET 4.0,SQL Server 2008,实体框架4.1

1 回答

  • 1

    我认为这是因为当您创建一个Setting对象时.Active字段采用的是默认值:即false . 因此,当您将Active设置为false时,没有任何更改 . 您必须在更新之前从Db加载实体 .

相关问题