在Post方法响应中获取ID,并自动生成ID

时间:2018-07-16 11:26:13

标签: json asp.net-web-api

尝试获取存储在Post请求数据库中的响应ID。在表ID是自动生成的。尝试了一个代码,但是没有用。 我没有在Json帖子中传递ID。请提出建议。使用list尝试并存储的数据是list1变量,但在该变量中,最后一个数据(该数据存储在Json中)位于该变量中。

public HttpResponseMessage Post(List<DelegateTable> delegatetable)
    {
        try
        {
            using (ShowContext delegateContext = new ShowContext())
            {
                DelegateTable delegates = new DelegateTable();
                List<DelegateTable> list1 = new List<DelegateTable>();

                foreach (DelegateTable item in delegatetable)
                {

                    delegates.Salutation__c = item.Salutation__c;
                    delegates.First_Name__c = item.First_Name__c;
                    delegates.Last_Name__c = item.Last_Name__c;
                    delegates.Account_Name__c = item.Account_Name__c;
                    delegates.Contact_Email__c = item.Contact_Email__c;
                    delegates.Category__c = item.Category__c;
                    delegates.Conference_Type__c = item.Conference_Type__c;
                    delegates.Conference_Selection__c = item.Conference_Selection__c;
                    delegates.Payment_Status_Interface__c = item.Payment_Status_Interface__c;
                    delegates.Barcode__c = item.Barcode__c;
                    delegateContext.delegates.Add(delegates);

                    delegateContext.SaveChanges();
                    list1.Add(delegates);

                }

                var message = Request.CreateResponse(HttpStatusCode.Created, list1);
                message.Headers.Location = new Uri(Request.RequestUri.ToString());
                return message;

            }

        }
        catch (Exception ex)
        {

            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        }
    }

我要传递的Json格式如下

 [
  {
    "attributes": {
      "type": "Registration__c",
      "url": "/services/data/v43.0/sobjects/Registration__c/a3hD0000001kE8YIAU"
    },
    "Salutation__c": null,
    "First_Name__c": "Test",
    "Last_Name__c": "Test",
    "Account_Name__c": "Test",
    "Contact_Email__c": "Test@gmail.com",
    "Category__c": "Test",
    "Conference_Type__c": null,
    "Conference_Selection__c": null,
    "Payment_Statuss__c": null,
    "Barcode__c": "Test"
  },
  {
    "attributes": {
      "type": "Registration__c",
      "url": "/services/data/v43.0/sobjects/Registration__c/a3hD0000001kEfOIAU"
    },
    "Salutation__c": "Mr.",
    "First_Name__c": "Demo",
    "Last_Name__c": "Demo",
    "Account_Name__c": "Demo",
    "Contact_Email__c": "Demo@yahoo.com",
    "Category__c": "Demo",
    "Conference_Type__c": null,
    "Conference_Selection__c": null,
    "Payment_Statuss__c": null,
    "Barcode__c": "Demo"
  }
  ]

2 个答案:

答案 0 :(得分:0)

您可以在执行proxyContext.SaveChanges()之后再次查询数据库中的Saved更改,以获取生成的ID,指定项目的ID,并将其存储在每个变量的列表中,并将其存储在响应查询中。 希望我能正确理解您的问题。

答案 1 :(得分:0)

尝试以下代码从数据库中获取ID,但现在的问题是它正在从数据库中获取所有数据。我只想只显示当前插入数据库中的数据。在上一个代码中添加了此代码。

 string query = "Select * from DelegateTable";

                    SqlCommand cmd1 = new SqlCommand(query, conn);
                    SqlDataReader dr;
                    conn.Open();
                    dr = cmd1.ExecuteReader();
                    while (dr.Read())
                    {
                        DelegateTable newItem = new DelegateTable();
                        newItem.ID = Convert.ToInt32(dr["ID"]);
                        newItem.First_Name__c = dr["First_Name__c"].ToString().Trim();
                        newItem.Last_Name__c = dr["Last_Name__c"].ToString().Trim();
                        newItem.Account_Name__c = dr["Account_Name__c"].ToString().Trim();
                        newItem.Contact_Email__c = dr["Contact_Email__c"].ToString().Trim();
                        newItem.Category__c = dr["Category__c"].ToString().Trim();
                        newItem.Barcode__c = dr["Barcode__c"].ToString().Trim();
                        newItem.Conference_Type__c = dr["Conference_Type__c"].ToString().Trim();
                        newItem.Conference_Selection__c = dr["Conference_Selection__c"].ToString().Trim();
                        newItem.Payment_Status_Interface__c = dr["Payment_Status_Interface__c"].ToString().Trim();
                        list1.Add(newItem);
                    }
                        var message = Request.CreateResponse(HttpStatusCode.Created, list1);
                        message.Headers.Location = new Uri(Request.RequestUri.ToString());
                        return message;

下面是添加以上代码后的修改代码

public HttpResponseMessage Post(List<DelegateTable> delegatetable)
        {
            try
            {
                using (ShowContext delegateContext = new ShowContext())
                {
                    DelegateTable delegates = new DelegateTable();
                    List<DelegateTable> list1 = new List<DelegateTable>();

                    foreach (DelegateTable item in delegatetable)
                    {

                        delegates.Salutation__c = item.Salutation__c;
                        delegates.First_Name__c = item.First_Name__c;
                        delegates.Last_Name__c = item.Last_Name__c;
                        delegates.Account_Name__c = item.Account_Name__c;
                        delegates.Contact_Email__c = item.Contact_Email__c;
                        delegates.Category__c = item.Category__c;
                        delegates.Conference_Type__c = item.Conference_Type__c;
                        delegates.Conference_Selection__c = item.Conference_Selection__c;
                        delegates.Payment_Status_Interface__c = item.Payment_Status_Interface__c;
                        delegates.Barcode__c = item.Barcode__c;
                        delegateContext.delegates.Add(delegates);

                        delegateContext.SaveChanges();

                    }

                    string query = "Select * from DelegateTable";

                    SqlCommand cmd1 = new SqlCommand(query, conn);
                    SqlDataReader dr;
                    conn.Open();
                    dr = cmd1.ExecuteReader();
                    while (dr.Read())
                    {
                        DelegateTable newItem = new DelegateTable();
                        newItem.ID = Convert.ToInt32(dr["ID"]);
                        newItem.First_Name__c = dr["First_Name__c"].ToString().Trim();
                        newItem.Last_Name__c = dr["Last_Name__c"].ToString().Trim();
                        newItem.Account_Name__c = dr["Account_Name__c"].ToString().Trim();
                        newItem.Contact_Email__c = dr["Contact_Email__c"].ToString().Trim();
                        newItem.Category__c = dr["Category__c"].ToString().Trim();
                        newItem.Barcode__c = dr["Barcode__c"].ToString().Trim();
                        newItem.Conference_Type__c = dr["Conference_Type__c"].ToString().Trim();
                        newItem.Conference_Selection__c = dr["Conference_Selection__c"].ToString().Trim();
                        newItem.Payment_Status_Interface__c = dr["Payment_Status_Interface__c"].ToString().Trim();
                        list1.Add(newItem);
                    }
                        var message = Request.CreateResponse(HttpStatusCode.Created, list1);
                        message.Headers.Location = new Uri(Request.RequestUri.ToString());
                        return message;

                }

            }
            catch (Exception ex)
            {

                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }