How to get the identifier created in Linq / Entity

I have two tables:

**Complaint**
-Id
-CreatedBy
-CreatedDate
....

**Solution**
-Id
-ComplaintId

Sometimes a complaint has an instant solution, which means that when it is created, a solution is also created. The database is Oracle, and to insert a new record into the database, I set the StoredGeneratePattern to Identity and use a trigger to insert the sequence value.

here my code:    

using (var context = new Entities())
        {
        var complaint = new Complaint
                            {
                                Title = TitleTextBox.Text.Trim(),
                                CreatedBy = CurrentUser.UserID,
                                Description = DescriptionTextBox.Text.Trim(),
                                ServiceId = Convert.ToDecimal(ddlService2.Value),
                                Contact = ContactTextBox.Text.Trim(),
                                CreatedDate = DateTime.Now,
                                Customer = txtUserName.Text.Trim(),
                                ResellerId = CurrentUser.ResellerID,
                                Status = ComplaintStatus.GetStatusCode("New complaint")
                            };
        if (CompletedCheckBox.Checked)
        {
            complaint.Status = ComplaintStatus.GetStatusCode("Completed");
            var solution = new Solution
                               {
                                   CreatedBy = CurrentUser.UserID,
                                   CreatedDate = DateTime.Now,
                                   SolutionDesc = DescriptionTextBox.Text,
                                   ComplaintId = complaint.Id
                               };            
            context.Solutions.AddObject(solution);

        }

            context.Complaints.AddObject(complaint);
            if(context.SaveChanges() > 0)
            {
                ResetFrom();
                return true;
            }

        }

the problem is that I cannot get the identifier of the newly created complaint in order to set the field in the solution. How can i do this?

Thank.

+3
source share
5 answers

Failed to complete the first operation call SaveChanges()and then request the complaint object, which should now have a complaint identifier.

+1
source

, / Oracle, , , . , Id , .

+1

SaveChanges(), , Identity, SaveChanges() .

context.Complaints.AddObject(complaint);


if (CompletedCheckBox.Checked)
    {

        complaint.Status = ComplaintStatus.GetStatusCode("Completed");
        context.SaveChanges();

        var solution = new Solution
                           {
                               CreatedBy = CurrentUser.UserID,
                               CreatedDate = DateTime.Now,
                               SolutionDesc = DescriptionTextBox.Text,
                               ComplaintId = complaint.Id
                           };            
        context.Solutions.AddObject(solution);

    }


        if(context.SaveChanges() > 0)
        {
            ResetFrom();
            return true;
        }

, , ComplaintId, .Complaint = , .

0

. , ( , ), , , :

, Complaint Insert ( DevArt, , , UpdateMode - , UpdateOnInsert, ),

, :

using (var context = new MyContext())
{
  var complaint = new Complaint {...};
  context.Complaints.AddObject(complaint);
  var solution = new Solution {..., Complaint = complaint};
  context.Solutions.AddObject(solution);
  context.SaveChanges();
}

SaveChanges , . , .

0

"".

So, create your solution object, as you do, do the following:

Soltion.Complaint = newCreatedComplaintObject;
0
source

All Articles