I'm building a small db-cleaner app for a QA sql server database. Naturally, I need to delete table rows with dependencies on them.
T-SQL cascading abilities are very limited, so I've tried using NHibernate to simplify matters. But, the only way I found for this was to create a collection for each dependency
in the object-to-delete, and mark that as cascade=delete.
That means creating many, many collections (both in the hbm file and in the C# object) which I don't need for any other purpose. Which makes this method as complicated as just using SQL.
Am I missing something? Is there any easier, more generic way to perform delete-cascade?
Thanks.
EDIT: Just to be clear, I avoid changing the foreign keys in the DB because it's a QA DB, designed to be identical to the production DB.
Eventually I found out a generic way to do the deletion:
This guy wrote a recursive SP which does all the work for you:
http://www.sqlteam.com/article/performing-a-cascade-delete-in-sql-server-7
Needed a little touch-ups (since my DB uses schemas) but works like a charm.
I suppose you have foreign keys defined between related tables in your database ?
You can specify at the foreign key level what should happen with related records when a parent record is being removed.
Check out MSDN for the cascading options, and how to define them:
Cascading FK constraints
Foreign Key Constraints
Related
I'm setting up a data warehouse (in SQL Server) together with our engineers we got almost everything up and running. Our main application also uses SQL Server as backend, and aims to be code first while using the entity framework. In most tables we added a column like updatedAt to allow for incremental loading to our data warehouse, but there is a many-to-many association table created by the entity framework which we cannot modify. The table consists of two GUID columns with a composite key, so they are not iterable like an incrementing integer or dates. We are now basically figuring out the options on how to enable incremental load on this table, but there is little information to be found.
After searching for a while I mostly came across posts which explained how it's not possible to manually add columns (such as updatedAt) to the association table, such as here Create code first, many to many, with additional fields in association table. Suggestions are to split out the table into two one-to-many tables. We would like to prevent this if possible.
Another potential option would be to turn on change data capture on the server, but that would potentially defeat the purpose of code first in the application.
Another thought was to add a column in the database itself, not in code, with a default value of the current datetime. But that might also be impossible / non compatible with the entity framework, as well as defeating the code first principle.
Are we missing anything? Are there other solutions for this? The ideal solution would be a code first solution, or a solution in the ETL process without affecting the base application, without changing too much. Any suggestions are appreciated.
I am working on a project where I may not alter the database in any way (unfortunately). I have started the project using Entity Framework and this has worked for the first few objects that I need. Now I have come across two scenarios that I am not sure how to accommodate.
Tables without a primary key defined.
Tables using the suffix of the table name as a field.
For the first, I get an error about reviewing my schema and uncommenting the proper area of the edmx file. The table has a field that acts as primary key but is not designated as not null and does not have a primary key created for it.
For the second, there are several tables with names like order1, order2, order3 etc where the table that needs to be accessed would be a parameter of my access methods.
My thought is that it would be simplest to just manually write the SQL and bind the data to models. If I go that route would I even use EF or do I just create a new database connection? what would be the 'proper' way to go about that?
Is there anyway to implement in-memory or fixed/hardcoded object instances in NHibernate that appear to all intents and purposes to be real instances of the object read from the database?
I have a historical database that has a number of missing foreign key values against a number of different tables as they are fixed/hard coded in the old DAL.
This is causing me problems in my NHibernate mapping.
An example of this would be a fixed immutable user, say 'ADMIN' that exists in code but not in the database. This 'ADMIN' user is still used in various foreign keys so needs to exist in NHibernate so that it can manage the FK mapping.
I've managed cheat loading by using a sql view which has the hard coded rows explicitly added, but of course I can't write to a view like that so need an alternative solution.
I did find a reference to the uNhAddIns WellKnowInstanceType that seems to do something similar, but I couldn't get to to work.
Anyone have any alternative suggestions?
one trick i can think of is attaching the imaginary User instance to the session befor querying using sess.Lock(admin, LockMode.None); that should take care of the reference. But I#m not sure what happens when eager loading the reference.
I'm using LinqPad to query a MySQL MyISAM database. There are foreign keys, but no constraints on them, so LinqPad can't pick up the relationship. I'd like to add these relationships (tell it which fields are FKs) to make querying easier. Is this possible?
There's no way to do this other than adding the foreign key constraints. (As a matter of interest, what's the reason for not having foreign key constraints?)
If there aren't FKs set up, then you only have one option.
You must join your tables manually using LINQ.
If you really want to use dot-notation, I think you can change your statement type to "C# Program" and put the LINQ in an extension method.
Does LINQ to SQL provide out-of-the-box polymorphic associations as ruby on rails active record does? If not, is there any workaround to manually map those associations?
Agreed. I found no possible way of doing this nor using the designer nor by hand appending class/method attributes. Moreover is not possible to have foreign key constraints for polymorphic associations. I discarded this option, thanks.
EDITTED
SQL Server won't allow you to have a foreign key relationship on a column that is not a primary key or doesn't have a unique constraint (or index) on it. There doesn't seem to be any restriction on having multiple tables use the same column in the child table as the foreign key. The DBML designer does discover these relationships and will create associations to both parent tables when you import the table. It appears however, that the designer-generated code will only be generated for one of the associations. That is, the designer shows the associations properly, but the code for all but one of them is omitted. Further, the extensibility methods and property settors don't seem to get defined properly in the designer-generated code either.
The same seems to be true if you add the associations by hand in the designer. Only one of the actual associations is implemented in the code and the other parent class's code seems irretrievably broken. It's possible that you may be able to use partial class implementations to add in the required functionality to match what the designer would generate, but I haven't tried this.
Also, LINQ2SQL doesn't support many-to-many relationships out of the box. You're limited to 1-1 and 1-many without writing the code yourself.