Entity Framework 4.2: InsertFunction for Tables without PK - sql-server

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.

Related

Orocommerce Delete an Entity

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

EF Code First Migrations and Foreign Key Constraints

Once FK Constraints have been created via Entity Framework Core 1.1 with Migrations in an ASP.NET Core Code First app, would it be ok to temporarily enable/disable the constraints directly in SQL Server without using EF - would it break the migrations created via EF etc? Background: I need to truncate data from a table that has been referenced by several tables via FK's that were created through EF Code First. SQL Server, as expected, complains that you can truncate the table since it's been referenced by a FK etc.
No, it would not break migrations. If you are doing anything on database and then reverting back the schema design of database to earlier state then migration will just run fine. Migrations when applied expect that shape of EF managed objects in database to be remain same (as it would have known earlier). Any temporary change is invisible to migration. And state needs to be same afterwards because when future migrations applied then appropriate objects are present otherwise DDL can cause failure.
Anything you change in the database schema in SQL Server, without the code, will break the migrations. You should delete the foreign key references in the code for the operation you want to do and then recreate them later. Though, be careful, if your data is left in inconsistent state, you might not be able to recreate constraints without losing data.

Entity Framework referential integrity not being enforced

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.

How do you create a composite key for a View in SQL Server 2008?

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.

Do i need to define relationship in SQL Server database if i am using EF and Linq

I am working on a asp.net mvc project and using Linq for all my data related operations. In such cases, do I need to define relationship in the SQL Server database ??
I am using Entity framework and Linq so I think it doesn't matter if I create an E-R diagram(i.e defining relationship) or not.
By 'define relationship' I assume you mean 'Foreign key constraints'. It is always advisable to define constraints in order to maintain data integrity. You should also consider that the database for your project could probably be used by other applications in the future which might be based on other technologies than EF.
If you have relations in DB they will automatically be imported in your EF Model but you always have an option to create the associations manually in the EF Designer if you don't have relations in DB.
See How to manually add association in Entity Framework

Resources