disable foreign key constraint in MSAccess - c#

During data transfer i want to disable/enable
All foreign keys on table
All foreign keys on all tables
via query in MSAccess.
I will call query it from C# Module. There will be bulk insertion.

You could delete your relationship from MSysRelationships do your stuff, make sure it is all valid then recreate the record in MSysRelationships.
This seems difficult though. You're knowingly putting bad data into a table with constraints. Why not put your data into a temp table with the same design as your table with the constraints then use an insert query to move the records in to the canonical table according to the rules you've established in the relationships. That way you don't ever drop relationships and risk ruining your table with bad data.

Related

Bulk Insert DataSet with multiple related tables into SQL Server C#

I have a DataSet with multiple tables the source document is an XML file.
mydataset.ReadXML(filename);
There are multiple tables with multiple relations. I have the database in the SQL created via code - tables, columns and relations.
Now I would like to insert the data . (Yes I want everything).
EDIT: The SQL tables use Identity Columns autogenerate - because I am not sure the incoming data will never duplicate one of the parameters that I would like to assume is unique.
So what methods should I do to ensure data is input considering I have foreign key constraints , how should I iterate the the tables in the dataset to make sure I don't try to insert in tables requiring an existing id. Do I create a hard coded map (I prefer not too) , or do I walk the tables looking for a Foreign key and verify the parent table etc..
Any Ideas ? I am sure someone has done this before and has a simple answer for me.
You have a couple of options. Assuming the database is not generating the key values, this is pretty simple. Either
1) You discover the order to load the tables so that each table with a Foreign Key is loaded after the table to which it refers.
2) You turn off constraint checking in SqlBulkCopy, and then optionally check the constraints after loading.
To check the constraints after load, run a command like
alter table SomeTable with check check constraint fk_SomeTable_OtherTable
If you do have database-generated keys, this is all harder. But the best way to tackle that is to use SEQUENCE objects isntead of IDENTITY columns and run sp_sequence_get_range to fetch the new key ranges to the client and apply the keys there first.

Creating abnormal database queries with Entity Framework

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?

SqlMetal cannot generate new foreign key associations to certain tables

My team are using SqlMetal to generate database classes from a SqlServer database. It works perfectly for all of our existing classes and foreign key associations but it's refusing to generate the code for a particular new foreign key association that we want to add. This key is from an audit table to a global event table detailing the time the audit record was created and the user it was created by. Many similar foreign key associations between other audit tables and this global "event" table exist in the system and SqlMetal generates code for those associations.
I've tried resolving this problem by:
Dropping and recreating the table
Removing the primary key
Creating a new identical table with a different name
Removing all other fields from the table
Dumping the indexes
Performing a fresh database build
Renaming the foreign key
None of the above seem to resolve the problem. However, SqlMetal does correctly generate code for foreign key associations from this table to some (but not all) other tables in the system. The association between these two tables would only generate when I altered the original create table script to include the foreign key association rather than running it (or a new equivalent table) in later. Unfortunately, we need to be able to deploy this change as a script to our existing production database so this isn't an option. I've seen a couple of articles and forum posts mentioning similar problems but none seem to give any solution or even an explanation.

Update a record with a modified primary key with Linq-To-Entities?

I have a table with a composite primary key made up of 3 fields. It just so happens, that one of these fields can change, so what happens is that when I want to update a record in the database and I change field3 (part of the primary key), the entity framework can't find the record to update because I have changed one of the primary key fields. Is there something I can do to solve this with the following limitations?
I can't change the primary fields.
I can't add an identity field and make it primary.
In a nutshell, I can't modify the schema.
This is not good database design and it was designed a while ago for individual hardware systems, so that is why I can't change the schema, because these individual units expect the schema in a certain way, otherwise, I would have designed it differently.
I understand primary keys should be immutable, but is there some sort of trick or hack I can use to update an existing record with a modified primary key without changing the schema?

Delete rows from two tables atomically in SQL Server?

How do I delete rows from different tables atomically?
Table A has a primary key and foreign key into Table B.
The standard solution is to use ON DELETE CASCADE for your constraint.
Search for 'cascade' on that page.
If that is not an option, this SO question may interest you: In SQL Server 2005, can I do a cascade delete without setting the property on my tables?

Categories