Entity Framework - Cascade Delete not getting set in Entity Model - c#

I am using EF 4.0 and am generating my Entity Model (.edmx) file from my database. I have several foreign key constraints for Cascade OnDelete, however, these are not getting set on my associations in my entity model. I click on the association and it shows End1 OnDelete: None.
When I check the Delete rules in my SQL Server 2008 database on my foreign key constraint, it says 'Cascade'.
Is there something I am missing to get the Cascade OnDelete flag to set in my entity model?

This answer
"I was having the same problem with SQL Server. When I tried to update mode from database, it didn't pick up the cascade rules. Note that the rules were added after the model was already created. I even tried deleting a table from the model and adding it back in. That had the same effect - no cascade rules.
However, when I created a brand new model with the same exact tables, it picked up the cascade rules. So my solution was just to delete the old model and create a new one with the same name, etc.
I guess is that there is something wrong with the update model from database process."
from this thread worked for me.

Related

When changing the cascading behavior of an FK in my database need to update model?

I work on MVC project with entity framework database first and I change the cascading behavior on an existing relationship: on delete cascade, and another one on delete set null.
Do I need to update the EF model with these changes, or will the old model continue to work?
EF will try to keep it's in-memory objects in sync with the database rows by applying the cascading rules that have been configured in the model.
Therefore, while not strictly required, it's a best practice to keep your EF model and database synchronized on the cascading behaviors. Otherwise EF can retain objects in memory that have been modified or deleted in the database.
see Tip 33 – How cascade delete really works in EF
and Cascade Delete in EF Core

Relation chanage not getting reflected in entity model

Am very new to Entity Framework. I am using vs 2012 professional 2012 for building my mvc4 app.
I will try to explain problem in my limited knowledge
I have created Ado.net entity data model. After that I have chnaged db for setting relations. After "update model from database" the relations are not gettng added to model classes. So when I run my app errors like
The relationship 'x.FK_Privileges_Module' was not loaded because the type 'x.Privilege' is not available.
is shown
How can I solve this. I even tried adding manually the relation to corresponding . cs file and still getting error for every newly added relation.
How can I solve this?
You can remove entities from model designer (Press Yes when message box will ask) and then add entities again.
Note: To delete, select entities and press delete key
The edmx is not able to find a Primary Key on Privilege and therefore the foregin key is not valid.
Add a Primary Key on Privilege

Database Access Layer

I am using Visual Studio 2012 and i have created a .sdf and .edmx for my database.
I was wondering what would be the best way to set the relationships between the entities.
I have tried just setting the association with the multiplicity set and get an error saying that they are not mapped.
Also how do you set the foreign keys.
I am trying to create a estate agent like system to book appointments online and in branch. I am creating it in C#.
I Assume you are going Database first.
If so, if you just simply creat your foreign keys and constraints in your database and then try to add your entities again, then your edmx file will have the relationships and everything.

Foreign key contraints interpretation problem

I generate Entity Data Model in Visual studio from the database.
However, I noticed that it does not generate neither relationships nor navigation properties from some foreign keys. It occurs when the foreign key contsraints are defined with the disabled option Enforce Foreign Key Constraint, like in the following exhibit (from SSMS).
Is there any way to deal with this? Unfortunately I cannot alter my database schema.
You can add them manually from the designer but it can have some consequences depending on the version of entity framework yo are using.
If you simply add association from the toolbox it by default creates independent association. Independent association must be mapped to the database counterpart. It means that you must manually open EDMX and cheat EF by modifying SSDL (you will add relation to SSDL part manually which can be quite hard task - follow SSDL reference: AssociationSet and Association elements). Now you can map the relation in the Mapping details window of the designer (you will also have to modify entities because FK property mustn't be mapped to the entity when independent association is used and in case of many-to-many association you will have to remove entity for junction table). EF will believe that this relation exists in the database. Once you modify SSDL manually you cannot use Update from the database any more. It will delete all your manual changes.
If you are using EFv4 you can use foreign key association (complete differences between those two types is described here). Foreign key association is not mapped but it cannot be used to define many-to-many relation.
The problem will occur if data in the database doesn't enforce the relation (which can happen because FKs are disabled). In such case your application will not work and there will be no way around this except repairing data integrity in the database or removing the association from the model.
The best solution for you is turning on FKs in the database!
Unfortunately You have to add those by hand in the model. That's the power of OR Mapping. Model can look different (better) than database.

Entity Framework: Regeneration of changed tables (key changes)

I've got a pretty large DB hooked up to my Entity Framework. I've got one particular set of tables where a common key (fk) was deleted from the design. When I "update the model from the database", I get a series of errors about this now-missing key.
Why doesn't the update process accurately detect that this key was removed? Also, if I manually remove the property from the model, I just end up with other errors. I also tried to simply delete the table and re-add it, but now the table doesn't show up in the "Add" section of the update wizard.
So, short of deleting the entire thing and starting over, is there any way to recover gracefully? Has the EF team released any newer versions of this wizard that has some of these issues fixed in it already?
Entity Framework does get confused from time to time. What you need to do is delete the Entity Table from the model, then rename the table in SQL Server to TableName_Temp, refresh the model, now the Entity Table should be OK, so delete the entity table again rename it back to the original name and refresh the model once more. This time everything should be fine.
I know this is a messed up procedure but this is the only way I found to fix this issue without blowing away the entire edmx and starting from scratch. Currently at my organization we have a policy not to make changes to the EDMX unless absolutely necessary so that we can delete and recreate when ever needed without any problems.
Hope this helps!

Categories