Entity framework foreignkey - entity-framework-6

I am getting the following error, while clearing the child records and adding them back to the parent.
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.

You can not delete data if which is referenced to another table as foreign key, but you done through
Remove value from referenced table after then you can able to perform delete operation only
try this https://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-error-the-relationship-could-not-be-chang/

Related

Do i need a core data migration when i add a new model to my .xcdatamodeld file

i have a an .xcdatamodeld which already has 2 entities, i have added another entity into this file(am not sure whether this is the right way to add a new entity), anyways my question is, do i need to implement any kind of migrations to take care of users who already have the app installed on their devices.
Please note, i have not modified any columns or schema on already existing entities.
cheers.
You don't need to do migration if you just only adding new entity, assuming that it doesn't have relationship with the other. According to raywenderlich.com, you have to do migration for the following scenario:
Deleting entities, attributes or relationships.
Renaming entities, attributes or relationships using the renamingIdentifier.
Adding a new, optional attribute.
Adding a new, required attribute with a default value.
Changing an optional attribute to non-optional and specifying a default value.
Changing a non-optional attribute to optional.
Changing the entity hierarchy.
Adding a new parent entity and moving attributes up or down the hierarchy.
Changing a relationship from to-one to to-many.
Changing a relationship from non-ordered to-many to ordered to-many (and vice versa).

Update throws NullReferenceException on entity with multiple FK references to a table

I have a table Transactions with 4 Foreign Key references to an Employee table - all nullable INT fields. I create a record in Transactions with 3 of the Employee fields populated. If I then update the remaining null Employee field I get the error:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.GetOtherEndOfRelationship(IEntityWrapper wrappedEntity)
I found this SO question which helped me identify the cause, but I still don't understand WHY it happens - Can anyone explain the reason it occurs?
My workaround is to make sure all Employee FK fields are populated on creation of the Transactions.
EDIT - this is not a duplicate- it has NOTHING to do with a simple check for null.

EF SaveChanges Error

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_tblStmtTask_StmtBatchID". The conflict occurred in database "CRM", table "dbo.tblStmtBatch", column 'ID'.
The statement has been terminated
I"m getting this error while saving a modification to 'tblStmtTask' .. I understand that I have a foreign key to the 'tblStmtBatch', but I haven't a clue why changing an unrelated column in the task should have any impact on the batch.
How do I decipher what the root cause of this error actually is ?
Place a breakpoint on the update statement. The FK property will be empty. In order to find the step where it is set to empty your best bet is to breakpoint the point of creation of the entity and step through it so can pinpoint the place where the FK gets set to empty.
If the FK property is empty at creation there has been a problem with mapping. If the entity is being created with the DefaultModelBinder, make sure you have provided the FK property in your form as well, e.g. using the #Html.HiddenFor HTML helper.

Object Generated From Entity Model Does Not Create Nullable Foreign Keys

I have noticed some strange behavior when it comes to nullable foreign keys in my database. I have just started playing with the entity framework so I'm probably doing something wrong, but I cannot figure this one out.
Say I have the following two tables: (CountryID is a foreign key that is nullable)
When I create a new entity model I end up with this:
But the CountryID properties are set to Nullable (None) - instead of True. Obviously this is a really trivial example, but with a large database it would be difficult switch all of these manually. Is there any way to have the entity framework use a nullable int? for these foreign keys?

Simple one to many relation (association) fails in EF designer

I tried setting up a simple one-to-many relation in Entity Frameworks designer.
The tables are Category (1) and Transaction (N). Here's what I did:
Add "association"
End1 = Category, multiplicity 1, navigation property=Transaction
End2 = Transaction, multiplicity Many, navigation property = Category
Building it gave me the error "No mapping specified". Ok, makes sense. So I added this mapping:
Category
Category.CategoryID = Transaction.CategoryID
But the mapping designer also automatically adds a mapping for the Transaction table, which I cannot figure out how to delete or how to setup:
Transaction
Transaction.TransactionID = ???
Leaving it empty seems most valid, but that gives me: Error 3024 "Must specify mapping for all key properties (TransactionID)"
And trying to set it to a fake int property just hoping it's a compiler bug. But that gives me errors 3002 and 3003.
I dont get what to do. Isnt Associations meant to be used this way?
I suggest creating (or importing from the database) an entity for Catagory and an entity for Transaction. Add scalar properties to each as needed. Next, right-click on your entity, click Table Mapping, and map your entity properties to the table fields. For example, for the Category entity, map CategoryID field to a CategoryID property. Do the same for the other entity. THEN create the association.
Note that associations linked by exposed foreign keys do not have any mappings.
BTW, you'll probably want to add navigation properties as well.

Resources