Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occasional "The relationship could not be changed because one or more of the foreign-key properties is non-nullable." error #133

Open
dave0783 opened this issue Dec 12, 2016 · 2 comments
Labels

Comments

@dave0783
Copy link

I cannot re-produce this consistently, but I have gotten the following error when using this plugin:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

I was under the impression it had to do with my entities (See SO Question); however when disabling this plugin the issue is gone.

@bilal-fazlani
Copy link
Owner

If you could re-produce it, and find a concrete scenario when it happens, it will be possible to fix it quickly.

@jcarenton
Copy link

To reproduce the bug:

SQL Tables :

MyTable : ID int, DummyId int FK
DummyTable : ID int

MyTableEntity class :

public int ID { get; set; }
public DummyEntity Dummy { get; set; }
public int DummyId { get; set; }

Scenario :

If with a Mapper like AutoMapper you set Dummy property as NULL and you change or keep value for DummyId

  • IdentityDbContext save without problem the object (of course DummyId must exist in Dummy Table)

  • TrackerIdentityContext fail with message :

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

Workaround/good practice :

Prevent editing Reference property, with AutoMapper :

.ForMember(x => x.Dummy, o => o.Ignore())

Or/And don't expose Reference property on ViewModel ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants