Do not track changes on navigation property in EF - entity-framework

Some of my entities have navigation properties as they have a foreign key to another entity in the database. In my code, I've set the primary keys of the navigation properties to link the entities with each other.
The problem is that entity framework sees this as a new entity and tries to add it to the database with next error as a result:
Cannot insert explicit value for identity column in table 'xxx' when IDENTITY_INSERT is set to OFF.
Obviously, because the entity is new and not acquired from the database.
Is there a way in EF to remove the tracking of the entity?

Related

Entity Framework database first foreign key constraints on Delete

I'm trying to understand On Delete and On Update foreign key functions in Entity Framework.
I have a database first (sqlite) model consisting of two tables:
Control
ID
Name
ControlTypeID
and
ControlType
ID
Name
The foreign key constraints on ControlTypeID are set to "On Delete Set Default" and "On Update Cascade." Default is set to "1" on ControlTypeID
The tables are bound to a datagridview. When I delete a ControlType (say, ID=2) rather than Control.ControlTypeID being set to 1, it is set to null.
Does entity framework not preserve the database foreign key constraint rules when it constructs the entity model?

EF Core one-to-many without Foreign Key (FK)

Are there ways to make one-to-many (abstract) relation without using SQL foreign keys?
I know it could be made by joining 2 non-related tables. But is it possible to use EF Core default navigation tools?
I think you want that because you can't add a foreign-key to your entity
So, you can solve that so:
You have a entity
Group
and you want a
List<User>
but your User can't be assigned to one group.
So you create a
GroupUser
entity which has a foreign key for the
List<GroupUser>
and a navigation property to the User
This is called a many-to-many relationship

navigation from entity to all it's navigation foreign key objects

I have an entity which has several foreign keys (int) .
I want to check if the Foreign Key Objects exists in the DB.
I want to do it as much generic as possible, so i won't need to check for each entity it's navigation properties

Adding new parent table with Entity Framework Migrations

I'm trying to use migrations to add a parent table to an existing child table. For eg. I currently have User table, now I want to add a Department table that has a 1 to many relationship: Department has many User.
My questions, in automatic update, can I somehow seed the parent table before adding the FK so I can update all the children to this default seeded Department? If automatic update cannot do this, how do I accomplish this in code?
What I currently did: Made the FK nullable, created the Parent and seeded it, then update all child User FK to the parent. But now I cant change the FK not nullable because throws this error: Automatic migration was not applied because it would result in data loss.
Switching from nullable to non-nullable is considered data loss because after the migration, there is no way to tell which rows (if any) were null. If you are ok with this, you can call Update-Database with the -Force flag.
Another option would be to add a code-based migration that would:
Add the Departments table
Insert a default department
Add the required FK column to User with a default value of the inserted department

Entity Framework cascade delete parent row in self-referenced table

In my MS SQL Server 2008 database I have self-referenced table with categories for hierarchy (ID and ParentID). Table have a self Foreign Key constraint and "Instead of Delete" trigger to perform deleting the full node with its children.
To manage data I'm using Entity Framework (4.3), with the model generated from DB with self-tracking entities and ObjectContext (generated by VS template). EDM also have self-referenced association on "category" entity.
I am faced with problem when trying to delete any parent row that has at least one child row.
After I call:
Entity.MarkAsDeleted();
Context.SaveChanges();
In SQL Server Profiler I see that EF first generates an update statement to set ParentID of child row(s) to null and then deletes parent row! Of course cascade rule in DB doesn't work and child nodes remains both in EF context and DB.
I've tried to set association rule "On delete" to "Cascade" and to "None" but it doesn't make sense...
How can I perform a cascade delete in a self-referenced table with EF, or at least how to prevent EF from updating parent IDs of child rows?
PS: here I found exactly the same problem without answer (MSDN)