EF 插入数据库生成的密钥,忽略 DatabaseGeneratedOption.None
EF inserts a DB generated key, ignoring DatabaseGeneratedOption.None
我正在使用 EF 6.1.3 Code First。
我有2个类:
- 员工
- 决定
员工是从一个文件加载的,每个文件都包含一个唯一的 EmployeeID
。
我的应用程序允许用户输入并保存有关这些员工的新决策。
但是,当 EF 将新的 Decision 保存到 DBContext 时,EF 会用 DB 生成的值 .
覆盖 Employee 的 EmployeeID
所以,我添加属性 DatabaseGeneratedOption.None
如下,到 Employee.EmployeeID:
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
接下来,我删除了数据库,删除了所有迁移,并添加了一个新的迁移。
但是,此迁移将 identity
("Value indicating whether or not the DB will generate values for this column during insert") 设置为 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."}
...尽管上下文的 Local DBSet 中的 EmployeeID 是正确的(不为空,未生成)。
我一直在寻找解决方案,但我能想到的只是一个次优解决方法:添加另一个非关键属性 给员工。
我尝试过的其他方法,但无济于事:
使用自动迁移
将 [ForeignKey("EmployeeID")] 添加到 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)));
}
}
}
}
[等等]
您实际上将映射属性应用于支持变量 _employeeID,您需要将其应用于 属性:
public class Employee : INotifyPropertyChanged
{
private int _employeeID;
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EmployeeID
{
get { return _employeeID; }
set
{
//Code omitted
}
}
我正在使用 EF 6.1.3 Code First。
我有2个类:
- 员工
- 决定
员工是从一个文件加载的,每个文件都包含一个唯一的 EmployeeID
。
我的应用程序允许用户输入并保存有关这些员工的新决策。 但是,当 EF 将新的 Decision 保存到 DBContext 时,EF 会用 DB 生成的值 .
覆盖 Employee 的 EmployeeID所以,我添加属性 DatabaseGeneratedOption.None
如下,到 Employee.EmployeeID:
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
接下来,我删除了数据库,删除了所有迁移,并添加了一个新的迁移。
但是,此迁移将 identity
("Value indicating whether or not the DB will generate values for this column during insert") 设置为 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."}
...尽管上下文的 Local DBSet 中的 EmployeeID 是正确的(不为空,未生成)。
我一直在寻找解决方案,但我能想到的只是一个次优解决方法:添加另一个非关键属性 给员工。
我尝试过的其他方法,但无济于事:
使用自动迁移
将 [ForeignKey("EmployeeID")] 添加到 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)));
}
}
}
}
[等等]
您实际上将映射属性应用于支持变量 _employeeID,您需要将其应用于 属性:
public class Employee : INotifyPropertyChanged
{
private int _employeeID;
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EmployeeID
{
get { return _employeeID; }
set
{
//Code omitted
}
}