I have a page with GridView pulling some data from a SQL Server database via Linq-to-SQL.
I made use of the automatically-generated buttons for deleting. However, in order for the delete command to work properly, I need to somehow make sure that one table in relation with those records I want to delete, is also modified (the related record in it is also looked up and deleted).
Whats the easiest way to do this?
Thanks,
Ondrej
Define a foreign-key constraint with cascade delete.
Delete Rule
Specify what happens if a user tries to delete a row with data that is involved in a foreign key relationship:
No Action An error message tells the user that the deletion is not allowed and the DELETE is rolled back.
Cascade Deletes all rows containing data involved in the foreign key relationship.
Set Null Sets the value to null if all foreign key columns for the table can accept null values.
Related
Is it possible in SQLite to make an update instead of a delete within a trigger ?
I.e, I got these two tables:
CREATE TABLE author (authorid INTEGER PRIMARY KEY, temporal NUMERIC);
CREATE TABLE comment (id INTEGER PRIMARY KEY, text TEXT, authorid INTEGER, FOREIGN KEY(authorid) REFERENCES author(authorid));
When a deletion of an author is attempted and there's any comment referencing that author i want to update the "temporal" field and abort deletion.
I've tested different approaches with triggers but i have not found a way to do the two things, make the update and abort the delete. I can abort the delete (though in this case it's not necessary as it is enforced by the foreign key constraint) or make the update (though the delete will remove the record, so the update has no effect)
Aborting the deletion is possible only with using RAISE to generate an error, but this would have the consequence that any UPDATE gets rolled back.
You could make author a view and create several INSTEAD OF triggers that pass through most actions to the base table.
However, it would be much easier to handler the temporal logic in your application.
One of the database view I am trying to import using entity framework contains only two columns, one is an integer type of column and another one is an aggregate function. I am getting the following error.
The table/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 understand it is a known scenario and it can be fixed by either including a Key column in the view or modifying the edmx file manually.
I just wanted to know if there is some other solution other than the above two? I do not want to include an additional column in my query and making changes in edmx is not feasible as DB changes are very frequent and the edmx will be overwritten every time I update from db.
You can mark both properties as entity key directly in the designer but you must ensure that the composite value of these two properties will be always unique. If you cannot ensure that you must add another unique column anyway or you may have some other problems when working with such entity set.
We have a table(say T1) that is referenced by about 16 other tables with foreign keys in our SQL Server database. The data is accessed through an ASP.NET application with LINQToSQL. When the user tried to delete a record from T1 the statement would time out. So we decided to first delete the records from the tables that reference T1 and only then delete the record in T1. The problem is that deletion from T1 does not work as fast as expected.
My question is: is it normal that deletion from a table referenced by many other tables to be so time-consuming even if the record itself does not have any 'children' records?
EDIT: Apparently the cause for the timeout was not the delete itself but another query that retrieved data from the same DataContext. Thank you for your suggestions, I have marked as answer the suggestion to add indexes for all foreign keys because it improved our script's execution plan.
I suspect that you may need to look into the indexing on your child tables.
It sounds as if you FKs are set to Cascade Deletes, so I would suspect that some of your tables do not have an index that includes the key to the parent as the first in the index.
In this way your delete will be full scanning the child tables - even if you've already deleted the child records it will still check as you've still got the Cascade set.
When you define a relationship in DB, you can set the Delete rule as Cascade in SQL server. In this way, when you delete the record from the parent table, it will be automatically deleted from the child tables.
Please see the image below:
If it taking long time, you may have set other constraint that will slow
down the process of deletion.
Linq does not do bulk deletes if you're having it operate directly on the record set -- instead, it is probably deleting one record at a time.
To improve performance, use a stored procedure instead for any bulk insert, update or delete operations.
I am unable to understand the line in bold from this msdn page:-
http://msdn.microsoft.com/en-us/library/bb738618.aspx
SaveChanges can generate an UpdateException when an object added to the ObjectContext cannot be successfully created in the data source. This can happen if a row with the foreign key specified by the relationship already exists. When this occurs, you cannot use Refresh to update the added object in the object context. Instead, reload the object with a value of OverwriteChanges for MergeOption.
In a table, a foreign key column can have a single value multiple times. e.g. DepartmentID foreign key in Users Table: More than one User can have same DepartmentID foreign key.
So how can this cause an UpdateException ?
Easy. You have an entity in your context in the Added state with a PK value of something already in the DB. This is common when people try to use stub objects incorrectly. If you want more help than that, you need to isolate your problem and post your code.
I have web application where Iam using linq to business entites i have business data model.
the problem is :
I have table with one column that it dosen't allow null value, when I try to update this table the folloeing error arise:
error The property 'e.g Carrier' is part of the object's key information and cannot be modified
what I can do?
The easiest thing to do is add a second column to the table that has a unique key eg guid and create a read only property on the entity that corresponds to it.
Linq to business entites needs some kind of key to keep track of what to update in the database. Usually this is the primary key on the database table. If you dont have a primary key it cannot reliably update the database and will then send you an exception.
Also if there is no primary key explicitly set on the table linq to business entites will select one of the columns (think its the first column in the table but i could be wrong) to act as a primary key and will therefore not allow you to update it.