I am trying to delete an object from a table (NewClubProspect) that has reference rows in a table called NewClubProspectNewClubEmail
The error I am getting is
The DELETE statement conflicted with the
REFERENCE constrain
"FK_dbo.NewClubProspectNewClubEmail_dbo.NewClubProspect_NewClubProspect_Id"
The conflict occurred in database
"Reporting", table "dbo.NewClubProspectNewClubEmail", column 'NewClubProspect_Id'.
The statement has been terminated.
The reference table looks like this:
I can solve the problem this way but would rather not use straight SQL. I have removed the try/catches for brevity:
//Delete out of the reference table first
var sql = "delete from NewClubProspectNewClubEmail where NewClubProspect_Id = #p0";
var r = ReportingDBTasks.Query(sql,prospect.Id);
//Delete the parent object
db.NewClubProspects.Remove(prospect);
db.SaveChanges();
How can I delete from the reference table using EF instead of straight SQL?
Thanks
The simplest way would be to enable cascading deletes on the constraint. But I suppose cascades were turned off for a reason, so I'm pretty sure you still have to do this the old fashioned way and delete each one.
Assuming you have a navigation property, it should be something like:
foreach(var nce in prospect.NewClubEmails)
db.NewClubProspectNewClubEmails.Remove(nce);
db.NewClubProspects.Remove(prospect);
db.SaveChanges();
Related
In my database, I have a parent table and a child table with foreign key pointing to the parent.. at some point I have to clear all rows from both tables.
I used the following code in Entity Framework:
using (MuseumDBEntities db = new MuseumDBEntities())
{
db.Database.ExecuteSqlCommand("truncate table childTable");
db.Database.ExecuteSqlCommand("truncate table parentTable");
}
I get an exception at the second truncate because of foreign key, although I am clearing child table first.
What should I do? Is there another way to delete all rows of both tables?
I don't know if a foreach loop over all rows is practical.
That's SQL server's fault!
You have two ways to achieve your goal:
Drop the foreign keys, then truncate the table and then recreate the foreign key (I don't recommend this solution because it is too much work and usually not worth it)
Instead of Truncate use Delete (I usually use this method)
Your code will look like this:
using (MuseumDBEntities db = new MuseumDBEntities())
{
db.Database.ExecuteSqlCommand("Delete from childTable");
db.Database.ExecuteSqlCommand("Delete from parentTable");
}
Since the two delete statements have no conditions (no where clause), all rows will be deleted from the tables.
I am trying to delete a record from Database but for a specific row m not able to do it.
This is my Linq Query to delete it
FormSubmit formSubmit = db.FormSubmits.Find(id);
db.FormSubmits.Remove(formSubmit);
db.SaveChanges();
And i am getting an Error Like this
I know this is Because of Foreign Key but how to Solve it.In SQL We use NO CHECK but not getting in linq Query.
Not an expert on this, but this seems the solution in your case:
Specify the UpdateCheck = UpdateCheck.Never on your column in your entity. This will disable the update check and will most likely fix your issue.
Another option is to set cascade deletion on the table in the database.
I am using MYSql database and Entity Framework as an ORM to it.
I have two tables:
table A and table B. Table A has a field which is a foreign key on some field from table B.
And the constraints to this key are: Update:Restrict, Delete:Restrict
So the situation:
when I try to delete a record from table B everything works fine
when I try to delete a record from table B and this record is referenced by another record from table A it does nothing and it is expected ofc.
when I try to delete a record from table B, which is referenced, it does nothing but if I after this try to delete a record from table A which was referencing that record from table B it appears that both records are deleted. But only the record from table A should be deleted.
So I assume that there is some kind of caching which caches the unsuccessfull query and tries to execute it when possible.
In some time I`ll try to post some pictures if my words are very fuzzy =)
Ah, and the question: can anyone clear this situation?)
Resolved this issue.
No caching or such things =)
Just entity object which I tried to delete was marked with EntityState.Deleted and despite the exception throwed the state wasn`t rollbacked.
I have a problem in Linq. I am getting all the contents a in table when I query. But I don't want to load certain rows which are marked with some special key Y.
So, for this, I'm iterating and removing from my local copy those which are having special key Y.
Later on, when I submit changes, I get an error:
"An attempt was made to remove a relationship between a priceTable and a dataTable. However, one of the relationship's foreign keys (P.Id) cannot be set to null."
Why is it so? How can I alter the contents of a particular entity set without touching some of the rows?
I want the rows that are marked as Y not to be returned from the DB. I don't want use them in my c# at runtime.
Dennis is correct. If you don't required those records marked as 'Y', then use the where clause and exclude those records. Then you are free to modify and update the records back to the database without any issues.
sample where clause looks like below
var data = from p in context.Persons
where p.Required == "Y"
select p;
I'm using Linq to SQL for the Database operations, and am trying to perform insert operations in a VIEW, It throws the Error,
XXt threw exception: System.InvalidOperationException: Can't perform Create,
Update or Delete operations on 'Table(XXX)' because it has no primary key.
How to use LINQ to insert a record into View using C#?
Thanks.
You can insert/update into views as per Updatable Views here. Only one underlying table can be inserted/updated to or it will fail. To implement this functionality using LINQ, do the following;
In your .DBML file tag one (or more) of the columns in the view as a Primary Key
Ensure any mappings in the view you are expecting to insert/update are exposed simply as a link to the base table column. Example;
Insertable/Updatable columns cannot include;
SUM(BaseTable.ColumnName) as ColumnName
ISNULL(BaseTable.ColumName,0) as ColumnName
BaseTable.ColumnName1 + ' ' + BaseTable.ColumnName2 as ColumnName
But can include;
BaseTable.ColumnName
BaseTable.ColumnName as MyNewName
Tag any of the columns that are not direct mappings to the base table as Auto Generated Value in your .DBML.
Give it a shot. I am successfully using this technique to use views as the only objects i use for both reading/inserting/updating records.
Actually you can insert into a view..if the underlying view has one table then you can insert into it.
If it has more than one table..then u can use instead of triggers;
Also I have inserted a record into a view..in linq to sql. (i have just started learning linq myself).
I had to create a primary key on a view. using the designer and then set the auto sync for that field to never. that should do the trick..
We can do insert, update and delete operations using a VIEW in LINQ to SQL process. All that we need to ensure is: view should have primary key.
We can set primarykey for a field in the view.
open the .dbml file designer and select the field which you want to make as primarykey and press F4 (open properties window).
select true for Primary Key property of the selected field.
Now execute your program. It should work.
You cannot insert into a VIEW. You can only insert into a table.
You can do this - see below.
Auto-Sync must be 'OnInsert' for this field in dbml.
I assume you mean that instead of trying to insert a row into a View, you are trying to insert a row into a table. You do not insert rows into Views.
Having said that, L2S requires your tables to have primary keys, as the error message indicates. Once you create a primary key, and update your .DBML accordingly, you should be fine.
Randy