Using EF6, I have two tables in an SQL database and I've used database first to create the EF diagram from the database.
My database is missing a foreign key constraint between the PK of one table and a foreign key in the other.
Rather than add the foreign key constraint into the database I created it as an association in the EF model.
The association looks good in the EF model, it has the correct principal and dependant.
However if I delete a row from the principal table when dependants are present, referential integrity doesn't kick in and the principal row gets deleted.
I know I can just add the relationship into the SQL database and update model from database. But I'd like to understand the scope of functionality of EF and whether it should be possible to add a relationship in the EF model but not in the SQL database and for it to still work?
It seems to me that if the association (relationship) exists in EF but not in MSSQL, then the association is ignored for referential integrity when you delete a row via EF?
However if I delete a row from the principal table when dependents are present, referential integrity doesn't kick in and the principal row gets deleted.
Dependents are present in database, or are present in Context?
If you haven't loaded all the dependent entities in the context, there is no way for EF to delete them. It needs their key for the delete statement. It doesn't even know they exist if they are not in the context.
So, the only way for referential integrity to work would be if EF retrieves all related entities into the context, on delete of principal entity, so it could issue a delete statement for every one of them (even if there are thousands).
I think, though, if there are dependent entities present in context, EF will issue delete statement anyway (even if it expects db to do all the work).
Hope it helps.
Related
I wanted to delete the entity I created with Entity Management before, but it was just disabled, and then I made changes that I could not remember in the database related to this entity and sometimes I am having problems with this entity while updating the schema.
Is it possible to do this entity hard delete in orocommerce?
Is it possible to do this entity hard delete in orocommerce?
From the management console, you can only soft-delete custom entities.
To hard delete, it is required to write a schema migration that uses Oro\Bundle\EntityConfigBundle\Migration\RemoveTableQuery.
For example, here is the migration that drops an event entity and the related database table:
https://github.com/oroinc/platform/blob/4b2efdbd33792df7fb0da9831b184838591f00b4/src/Oro/Bundle/NotificationBundle/Migrations/Schema/v1_5/DropEventTable.php#L48-L49
I need to create a composite key for a view in SQL Server 2008 because I cannot import a view into entity framework without a primary key defined and since Views don't have primary keys I have to create. Right now I am getting this error when I try to import the View into EF.
The table/view 'FanDB.dbo.Quick_View' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.
I have been able to add the View to EF before but my boss deleted our DB and so I am recreating the DB from scratch. I have just forgotten how I did it:( Thanks for your help, feel free to ask any questions I will be checking back as often as I can and if I figure it out I will post the solution because all the questions on stack overflow either say to edit the XML file(can't do this because I can't add it to the .edmx) or add a primary key column which I can't do because it is a quick view.
You could recreate the view as a table, add a primary key, import it into EF, and then drop table and reinstate the view. This will get your table into EF, but then you have to be careful whenever you update the model again in the future.
Alternatively, you could create a model db, with all the same object names as your actual db, but with views created as tables. At design time, use the model db, and at run time, use the actual db. So long as the view is updateable, the EF runtime won't care that it's actually a view, and not a table.
If you are feeling compulsive, and have a lot a tables, it wouldn't be too hard to auto-magically generate the model db from the actual db.
In Entity Framework 4.2, I am trying to insert a value into a table which has no PK. EF gives me the following error:
Unable to update the EntitySet 'ConditionTypesForWebApplications'
because it has a DefiningQuery and no <InsertFunction> element exists
in the <ModificationFunctionMapping> element to support the current
operation.
From the below discussion, I understood that it is possible to tweak that by editing the edmx file but it is not a best case when you are working with DbContext class.
How do you update a table with a foreign key to another table in ADO.Net Entity Model?
Is there any better way of enabling this?
It is same for DbContext with EDMX (DbContext without EDMX doesn't support entities without keys at all). Entity without key is read only unless you manually change EDMX file (be aware that any update model from database will delete your changes unless you buy more advanced tools for EDMX editing) or you will create stored procedure and map it to insert function of the entity.
In short entity should have key because EF should be able to uniquely identify it. Otherwise you can meet other problems in your application.
I have two separate SQL Server 2005 databases (on the same server)
security database
main application database
The security database has a user table with everything needed to authenticate. -
The application database has a person table with extended user details. There is a 1-1 mapping between the security database user table and the application database person table.
I want to enforce a mapping between the user and the person table. I'm assuming that foreign keys can't be mapped across databases thus I am wondering what to do to enforce the integrity of the relationship.
Cross database foreign keys are indeed not supported
Msg 1763, Level 16, State 0, Line 2
Cross-database foreign key references are not supported.
If you really want to enforce the referential integrity on the database side you will have to rely on triggers. (which I don't recommend)
to make your code more maintainable you could create synonyms for the tables you want to check referential integrity on.
CREATE SYNONYM myTable FOR otherdatabase.dbo.myTable;
This would be to make the "manual" checks easier, as you can not create foreign keys on a synonym.
It's a lot of work but you may think about merging those two databases into a single database. If you want a logical difference between objects within the database, you can use a schema.
I am working on an old database, that i do not want to touch or modify in any way if possible.
I want to build a new application that uses it's data but the database has no actual relations despite having primary and foreign keys linking tables.
If i import these tables into an Entity Framework model and add the associations manually, will i be able to use things such as lazy loading and linq?
Many thanks,
Kohan
This is definitely possible. Entity Framework simply generates SQL queries containing joins or where clauses that reference columns that you define in your conceptual model as foreign keys. The generated SQL is directly executed by the database.
Primary and foreign keys are only in your database for referential integrity. As a very simple test you can execute a SQL statement directly in your database that joins two related tables that do not have a foreign key relationship. You'll see that the query simply works. Entity Framework does exactly the same when you correctly define the relationships in your model.