Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF inserts a DB generated key, ignoring DatabaseGeneratedOption.None

I'm using EF 6.1.3 Code First.

I have 2 classes:

  • Employee
  • Decision

Employees are loaded from a file, that includes a unique EmployeeID for each.

My application lets the user enter and save new Decisions regarding these Employees. However, when EF saves the new Decision to the DBContext, EF overwrites the Employee's EmployeeID with a DB generated value.

So, I added the attribute DatabaseGeneratedOption.None as follows, to Employee.EmployeeID:

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

Next, I deleted the DB, deleted all migrations, and added a new migration.

However, this migration sets identity ("Value indicating whether or not the DB will generate values for this column during insert") to true:

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

...so, that didn't help.

Editing this Migration manually (i.e., setting identity to false) causes an error to be thrown when trying to save the context: 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."}

...although the EmployeeID in the context's Local DBSet is correct (not null, not generated).

I've been looking high and low for a solution, but all I can think of is a sub-optimal work-around: adding another, non-key property to Employee.

Other things I've tried, to no avail:

  • using automatic migrations

  • adding [ForeignKey("EmployeeID")] to Decision.Employee


EDIT: As requested by E-Bat, here are excerpts from my entities:

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)));
                }
            }
        }
    }

[and so on]

   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)));
                    }
                }
            }
        }

[and so on]

like image 837
Jooj Avatar asked Jul 07 '26 09:07

Jooj


1 Answers

You actually applied the mapping attribute to the backing variable _employeeID, you need to get it applied to the property instead:

public class Employee : INotifyPropertyChanged
{
    private int _employeeID;
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EmployeeID
    {
        get { return _employeeID; }
        set
        {
         //Code omitted  
        }
    }
like image 95
E-Bat Avatar answered Jul 09 '26 23:07

E-Bat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!