EF插入数据库生成的密钥,忽略DatabaseGeneratedOption.None

时间:2015-12-23 16:03:21

标签: c# entity-framework ef-code-first primary-key ef-migrations

我使用的是EF 6.1.3 Code First。

我有2个班级:

  • 员工
  • 决策

员工是从一个文件加载的,每个文件都包含一个唯一的EmployeeID

我的应用程序允许用户输入并保存有关这些员工的新决策。 但是,当EF将新决策保存到DBContext时, EF会使用DB生成的值覆盖Employee的EmployeeID

因此,我将以下属性DatabaseGeneratedOption.None添加到Employee.EmployeeID:

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

接下来,我删除了数据库,删除了所有迁移,添加了新迁移

但是,此迁移设置identity("表示数据库是否会在插入&#34期间为此列生成值的值;)到true

CreateTable(
                "dbo.Employees",
                c => new
                    {
                        EmployeeID = c.Int(nullable: false, identity: true),

......所以,这没有用。

手动编辑此迁移(即,将identity设置为false)会在尝试保存上下文时引发错误: InnerException = {"Cannot insert the value NULL into column 'EmployeeID', table '[...]DBContext.dbo.Employees'; column does not allow nulls. INSERT fails. The statement has been terminated."}

...虽然上下文的本地DBSet中的EmployeeID是正确的(非空,未生成)。

我一直在寻找解决方案的高低,但我能想到的只是次优的解决方法:向Employee添加另一个非关键属性。< / p>

我试过的其他事情,但无济于事:

  • 使用自动迁移

  • 将[ForeignKey(&#34; EmployeeID&#34;)]添加到Decision.Employee

修改: 根据E-Bat的要求,以下是我的实体摘录:

public class Employee : INotifyPropertyChanged
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    private int _employeeID;
    public int EmployeeID
    {
        get { return _employeeID; }
        set
        {
            if (value != _employeeID)
            {
                _employeeID = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(nameof(EmployeeID)));
                }
            }
        }
    }

[等等]

   public class Decision : INotifyPropertyChanged
    {
        private int _decisionID;
        public int DecisionID
        {
            get { return _decisionID; }
            set
            {
                if (value != _decisionID)
                {
                    _decisionID = value;

                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(nameof(DecisionID)));
                    }
                }
            }
        }

        private Employee _employee;
        public Employee Employee
        {
            get { return _employee; }
            set
            {
                if (value != _employee)
                {
                    _employee = value;

                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(nameof(Employee)));
                    }
                }
            }
        }

[等等]

1 个答案:

答案 0 :(得分:1)

您实际将映射属性应用于支持变量 _employeeID ,您需要将其应用于该属性:

public class Employee : INotifyPropertyChanged
{
    private int _employeeID;
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EmployeeID
    {
        get { return _employeeID; }
        set
        {
         //Code omitted  
        }
    }