插入新实体时如何覆盖SQL Server默认值约束? [英] How to override SQL Server default value constraint on a boolean when inserting new entity?

查看:167
本文介绍了插入新实体时如何覆盖SQL Server默认值约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个名为 Department 的实体带有一个布尔值指定它是否是活动部门的值。这是实体定义的简化版本。对于 Active 属性,也没有流畅的映射。

  public class Department 
{
public Guid DepartmentId {get;组; }
public string DepartmentName {get;组; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public bool Active {get;组; }
}

在数据库中, Active 是一个位,不为空



我们在数据库中有一个默认约束活动列将新创建的条目设置为true。

  ALTER TABLE [ dbo]。[Department] ADD DEFAULT((1))FOR [Active] 

问题是当我创建一个新的实体时,用户可以选择将 Active 设置为false,但是当添加到上下文中时,该属性为false,但是在调用保存更改后,属性是真实的,在数据​​库记录中是正确的。

  Guid ICommonRepository.AddDepartment(Department newDepartment)
{
使用(var db = new CommonDbContext())
{
if(newDepartment.DepartmentId == Guid.Empty)
{
newDepartment.DepartmentId = Guid.NewGuid );
}
db.Departments.Add(newDepartment); // Here newDepartment.Active is false
db.SaveChanges();
return newDepartment.DepartmentId; // Here newDepartment.Active is true
}
}

有没有设置EF或可以在 Active 列中使用的注释,这将允许我指定false?

解决方案

Active 字段中删除 [DatabaseGenerated(DatabaseGeneratedOption.Computed)] 您的模型。



实体框架不会尝试将值保留到数据库,这就是为什么设置默认值。


This project is using Entity Framework code-first.

I have an entity called Department with a boolean value that specifies whether it is an active department. Here is a simplified version of the entity definition. There are also no fluent mappings for the Active property.

public class Department
{
    public Guid DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public bool Active { get; set; }
}

In the database the column for Active is a bit, not null.

We have a default constraint in the database for the Active column to set newly created entries to true.

ALTER TABLE [dbo].[Department] ADD  DEFAULT ((1)) FOR [Active]

The problem is that when I create a new entity the user can choose to set Active as false, but this property is false when it is added to the context, but after save changes is called, the property is true and it is true in the database record.

Guid ICommonRepository.AddDepartment(Department newDepartment)
{
    using(var db = new CommonDbContext())
    {
        if(newDepartment.DepartmentId == Guid.Empty)
        {
            newDepartment.DepartmentId = Guid.NewGuid();
        }
        db.Departments.Add(newDepartment); // Here newDepartment.Active is false
        db.SaveChanges();
        return newDepartment.DepartmentId; // Here newDepartment.Active is true
    }
}

Is there a setting in EF or an annotation that I can use on the Active column that will allow me to specify false?

解决方案

Remove [DatabaseGenerated(DatabaseGeneratedOption.Computed)] from the Active field of your Model.

Entity Framework will not try to persist a value to the database which is why the default is being set.

这篇关于插入新实体时如何覆盖SQL Server默认值约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆