简体   繁体   English

使用ASP.NET中的外键将数据插入SQL Server数据库时出错(找不到主键)

[英]Error inserting data into SQL Server database with foreign key in ASP.NET (could not find primary key)

I am using ASP.NET MVC2 in Visual Studio 2008. I believe the SQL Server is 2005. 我在Visual Studio 2008中使用ASP.NET MVC2。我相信SQL Server是2005。

I have two tables: EquipmentInventory and EquipmentRequested 我有两个表:EquipmentInventory和EquipmentRequested

  • EquipmentInventory has a primary key of sCode EquipmentInventory具有sCode的主键
  • EquipmentRequested has a foreign key called sCode based upon sCode in EquipmentInventory. EquipmentRequested具有一个基于EquipmentInventory中的sCode的外键sCode。

I am trying the following code (lots of non-relevent code removed): 我正在尝试以下代码(已删除大量非相关代码):

        try
        {
            EChODatabaseConnection myDB = new EChODatabaseConnection();

//this section of code works fine.  The data shows up in the database as expected
            foreach (var equip in oldData.RequestList)
            {
                if (equip.iCount > 0)
                {
                    dbEquipmentInventory dumbEquip = new dbEquipmentInventory();
                    dumbEquip.sCode = equip.sCodePrefix + newRequest.iRequestID + oldData.sRequestor;
                    myDB.AddTodbEquipmentInventorySet(dumbEquip);
                }
            }

            myDB.SaveChanges();   //save this out immediately so we can add in new requests

//this code runs fine
            foreach (var equip in oldData.RequestList)
            {
                if (equip.iCount > 0)
                {
                    dbEquipmentRequested reqEquip = new dbEquipmentRequested();
                    reqEquip.sCode = equip.sCodePrefix + newRequest.iRequestID + oldData.sRequestor;
                    myDB.AddTodbEquipmentRequestedSet(reqEquip);
                }
            }

//but when I try to save the above result, I get an error
            myDB.SaveChanges();

oldData is passed into the function. oldData传递到函数中。 newRequest is the result of adding to a "non-related" table. newRequest是添加到“非相关”表中的结果。 newRequest.iRequestID does have a value. newRequest.iRequestID确实有一个值。

In looking at the reqEquip is the watch window, I do notice that EquipInventory is null. 在查看reqEquip是监视窗口时,我确实注意到EquipInventory为空。

The error message I receive is: "Entities in 'EChODatabaseConnection.dbEquipmentRequestedSet' participate in the 'FK_EquipmentRequested_EquipmentInventory_sCode' relationship. 0 related 'EquipmentInventory' were found. 1 'EquipmentInventory' is expected." 我收到的错误消息是:“'EChODatabaseConnection.dbEquipmentRequestedSet'中的实体参与了'FK_EquipmentRequested_EquipmentInventory_sCode'关系。找到了0个相关的'EquipmentInventory'。预期为1个'EquipmentInventory'。”

Obviously I'm doing something wrong but thus far, I can not seem to find where I am having a problem. 显然我做错了,但是到目前为止,我似乎找不到我遇到问题的地方。

Anyone have some hints on how to properly insert a record into a table that has a foreign key reference? 任何人都对如何正确地将记录插入具有外键引用的表中有一些提示?

UPDATE: 更新:
I am using the Data Entity Framework. 我正在使用数据实体框架。

UPDATE: 更新:
Thanks to Rob's answer, I was able to figure out my error. 多亏了Rob的回答,我才能够找出我的错误。 As Rob mentioned, I needed to set my reference for the foreign key. 如Rob所述,我需要为外键设置参考。

My coding result looks like: 我的编码结果如下:

            foreach (var equip in oldData.RequestList)
            {
                if (equip.iCount > 0)
                {
                    dbEquipmentInventory dumbEquip = new dbEquipmentInventory();
                    dumbEquip.sCode = equip.sCodePrefix + newRequest.iRequestID + oldData.sRequestor;
                    myDB.AddTodbEquipmentInventorySet(dumbEquip);

                    //add in our actual request items
                    dbEquipmentRequested reqEquip = new dbEquipmentRequested();
                    reqEquip.EquipmentInventory = dumbEquip;
                    myDB.AddTodbEquipmentRequestedSet(reqEquip);
                }
            }

            myDB.SaveChanges();   

Does anyone see a better method for doing this? 有谁看到这样做的更好方法?

What are you using as an ORM? 您正在使用什么作为ORM? I believe that regardless of which one you're using, you could use the foreign key handling of most ORMs to handle this for you. 我相信,无论您使用的是哪种,都可以使用大多数ORM的外键处理来为您处理。 For example, you make a new dumbEquip, don't do the immediate save. 例如,您制作了一个新的dumbEquip,请勿立即保存。 Do your dbEquipmentRequested reqEquip = new dbEquipmentRequested(); 是否执行dbEquipmentRequested reqEquip = new dbEquipmentRequested(); and add the data to it and then say dumbEquip.dbEquipmentRequested.Add(reqEquip). 并向其中添加数据,然后说出dumbEquip.dbEquipmentRequested.Add(reqEquip)。 Then save the record and the ORM should save the records in the correct order required for the FK and even enter the FK ID into the reqEquip record. 然后保存记录,ORM应该以FK所需的正确顺序保存记录,甚至将FK ID输入到reqEquip记录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM