【发布时间】:2021-12-25 07:26:20
【问题描述】:
我添加了一个新列 IsForceLogOff(数据类型 = 位)。当我以通常的方式更新表格时,除了新添加的 bool 列之外,所有内容都会更新。
public static UserErrorStatus UserUpdate(User user, Company company)
{
UserErrorStatus status = UserErrorStatus.Error;
using (OAPDataLayerEntities DbEntity = GetDBContext())
{
try
{
using (TransactionScope transaction = new TransactionScope())
{
user.IsForceLogOff = true;
DbEntity.Users.Attach(user);
DbEntity.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
DbEntity.SaveChanges();
transaction.Complete();
DbEntity.AcceptAllChanges();
status = UserErrorStatus.Success;
}
}
}
}
创建表语句:
CREATE TABLE [dbo].[User]
(
[UserID] [int] IDENTITY(1,1) NOT NULL,
[AddressID] [int] NULL,
[AccountTypeID] [int] NOT NULL,
[StaffID] [int] NULL,
[SalutationID] [int] NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[EmailAddress] [nvarchar](100) NOT NULL,
[Password] [nvarchar](50) NOT NULL,
[SecurityQuestionID] [int] NOT NULL,
[SecurityAnswer] [nvarchar](50) NOT NULL,
[PhoneNumber1] [nvarchar](50) NULL,
[PhoneNumber2] [nvarchar](50) NULL,
[Fax] [nvarchar](50) NULL,
[CompanyID] [int] NULL,
[DateCreated] [smalldatetime] NOT NULL,
[DateModified] [smalldatetime] NOT NULL,
[DateLastLogin] [smalldatetime] NOT NULL,
[UserIDModified] [int] NULL,
[StatusID] [int] NOT NULL,
[Notes] [ntext] NULL,
[IsForceLogOff] [bit] NOT NULL
)
参考上面的sql
【问题讨论】:
-
向我们展示 CREATE TABLE 表定义、用户实体和用户配置
-
@CaiusJard 用实体详细信息的截图更新了问题
-
我更希望右键单击表格>>脚本作为>>创建>>到剪贴板,然后粘贴文本..另外,我要求了 3 件事,你提供了 1
-
@CaiusJard 我已经更新了这个问题。请注意,我如何更新表格而不是表格结构存在问题。感谢您对我编写的用于更新的 C# 代码的回答
-
是的.. 提供表创建允许我们 a) 查看默认值是否会带来麻烦 b) 准确复制您的表,以便我们可以轻松地在我们的机器中创建一个并尝试重现/修复你的问题
标签: c# .net asp.net-mvc entity-framework