I am using Linq-to-SQL with MVC 3.0. I have a master table associated to a values table through a foreign key (foreign key is the valus table id stored in the master table).
When the values table id in the master is changed Linq-to-SQL creates a new (unwanted) row in the values table with all of the columns identical to the selected values row but with a new auto id.
Tracing the code and monitoring the SQL Server table's rows reveals that the the new row is generated from the repository at db.SubmitChanges(); but tracing through designer.cs it's not evident how the unwanted row is added.
Related
I'm trying to change some old deprecated code on how items are saved in my game. The current method is deleting all items, then inserting all items, then inserting equipment (child table of the items). It has a pretty big performance impact and I was wondering if changing the INSERTS to INSERTS ON DUPLICATE KEY UPDATE would have a noticeable impact on performance.
If it does, then I have a follow-up question. My plan is to load the items in with their original inventoryitemid and use that as the key to save with later.
The issue is when executing this following statement:
INSERT INTO inventoryitems (inventoryitemid, itemid) VALUES (?, ?) ON DUPLICATE KEY UPDATE ...
may miss some conditions. What I would like is for MySQL to INSERT if it doesn't exist with the default value (auto increment), otherwise UPDATE. At the moment new items are generated with an inventoryitemid at 0 since keys are generated on INSERT anyways.
tl;dr: I need a way to INSERT ON DUPLICATE KEY UPDATE without having the inventoryitemid beforehand (since new items are generated in-game with inventoryitemid of 0). At the moment I have to specify what the inventoryitemid is beforehand and I might not have access to that information.
First goal and issue
Try to insert a new item that doesn't exist in the database without having the inventoryitemid beforehand.
Item isn't inserted into the database with the next incremented db value.
Second goal (no issue)
Attempting to insert item into database with an existing inventory itemid
Database updates the item in the database successfully (yay!)
Trying out solution: Inserting value with NULL to try to trigger the autoincrement
When you're inserting a new row, specify inventoryitemid = NULL. This will create a new row because it's not a duplicate key, and it will auto-increment the inventoryitemid.
After you insert a new item like this, you can use LAST_INSERT_ID() to get the inventoryitemid that was assign to it. Then you can send that to the game code so it will know that the new item has this ID. It can then use this ID in future updates, instead of sending NULL.
When you're modifying an existing row, specify the value of its inventoryitemid. Then the ON DUPLICATE KEY UPDATE code will replace it instead of inserting a new row.
I am stuck with a scenario in SSIS. I have two table both maintain the referential integrity means one is parent table and another is child. When I insert the some records in parent table then those new records should be insert into child table.
Please share if you have any idea to implement this in SSIS.
From your description I take for granted, that you know WHICH child records have to be cretaed for WHICH master record, so I guess there are basiacally two possibilities.
All data is known at runtime:
Create a dataflow, which populates the master table
Cretae a second dataflow, which populates the child table and connect it to the first dataflow via Succeed constraint
But basically this sounds bit too easy - guess you tried this out already? So here comes possibility 2: the parent table creates IDs, which have to be referenced by the child records:
Again we start with one dataflow in order to populate the master table
then we add a second dataflow - again attached to the first one via succeed constraint
in this dataflow we add a lookup, which checks the master table for the corresponding IDs
This ID is then written to the corresponding column in the child table.
If I have Visual Studio ASP.NET project. In my project I have a database with 2 tables, parent table with an ID as a primary key and child table with an ID as a foreign key referencing to parent table's primary key. In my child table I keep the records, but I also want to be able to display those records belonging to the right ID so I don't fill my child table with wrong data. What would be the way to do that? Can it be done via some sql statements or only stored procedure?
Apologies for the noob question (I'm keenly learning as I go). I'd be grateful for some advice on the Primary Key.
I have 5 separate (unrelated) tables (Access 2003) containing similar fields that I will be merging (using Append queries) into a single new table. Each record between tables is unique (no duplicated).
Each separate table already has a primary key field using the default autonumber method (1-n). This means (I'm thinking) that there will be many duplicate primary key numbers between tables.
Is it standard practice (and ok to do) to detete the existing primary key field and create a new (autonumber; 1-n) upon merging. Should I do this before the merge (for each separate table) or after the merge (on the single new table)?
Create your new table with the table structure, primary keys and any other necessary metadata defined. Then run a SELECT INTO statement from each of the five table tables specifying the columns to copy into the new table. Since you already have your identity column defined on the new table and you are not selecting the identity column on the old table(s) the data should copy over and the insert will assign a new primary key value.
I have a many-to-many relation in 3 tables: ProgramUserGroup and Feature are the two main tables, and the link between them is LinkFeatureWithProgramUserGroup, where I have Foreign key relations to the two parent tables.
I have a dataset with inserts: I want to add a new row to ProgramUserGroup, and a related (existing) Feature to the LinkFeatureWithProgramUserGroup table.
When Inserting new rows, I'm setting the default id to -1:
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> <DataSetUserGroup xmlns="http://tempuri.org/DataSetUserGroup.xsd">
<ProgramUserGroup diffgr:id="ProgramUserGroup1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<id>-1</id>
<Name>99999999999</Name>
<Active>false</Active>
</ProgramUserGroup>
<LinkFeatureWithProgramUserGroup diffgr:id="LinkFeatureWithProgramUserGroup1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<id>-1</id>
<Feature_id>7</Feature_id>
<ProgramUserGroup_id>-1</ProgramUserGroup_id>
</LinkFeatureWithProgramUserGroup> </DataSetUserGroup> </diffgr:diffgram>
while I'm updating the tables, I get an error:
"The INSERT statement conflicted with the FOREIGN KEY constraint "FK-LinkFeatu-Progr-7DCDAAA2". The conflict occurred in database "x", table "dbo.ProgramUserGroup", column 'id'."
The code for the update is the following:
DataSetUserGroupTableAdapters.LinkFeatureWithProgramUserGroupTableAdapter lfa = new LinkFeatureWithProgramUserGroupTableAdapter();
DataSetUserGroupTableAdapters.ProgramUserGroupTableAdapter pug = new ProgramUserGroupTableAdapter();
pug.Update(dsUserGroup.ProgramUserGroup);
lfa.Update(dsUserGroup.LinkFeatureWithProgramUserGroup);
if I check the ProgramUserGroup table's new row's ID, it has been updated from -1 to ##identity (like 1099), so it's okay - it inserts the new row.
But In the LinkFeatureWithProgramUserGroup table, the related ProgramUserGroup.ID value is still -1, it was not updated anyhow.
How could I force the update of the link table's keys as well?
I've tried
pug.Update(dsUserGroup.ProgramUserGroup);
dsUserGroup.Merge(dsUserGroup.ProgramUserGroup);
lfa.Update(dsUserGroup.LinkFeatureWithProgramUserGroup);
But didn't solve the problem :(
Thanks,
b.
Yes, there's a work-around for this.
You need to tell your parent table's table-adapter to refresh the
data-table after update operation.
This is how you can do that.
Open the properties of ProgramUserGroupTableAdapter -> Default Select Query -> Advnaced options. and Check the option of Refresh the data table. Save the adapter now. Now when you call update on table-adapter, the data-table will be updated [refreshed] after the update operation and will reflect the latest values from database table. if the primary-key or any coloumn is set to auto-increment, the data-table will have those latest value post recent update.
Now you can Call the update as pug.Update(dsUserGroup.ProgramUserGroup);
Read latest values from the ProgramUserGroup coloumns and assign respective values into the child table before update. This will work exactly the way you want.
alt text http://ruchitsurati.net/files/tds1.png