-1

Am trying to update records and save changes to the database and it seems as i can't because i cannot get my list. sorry if my question is not clear.

Let me know if there's other way i can do it.

List<ClaimHistoryModel.ClientLog> ClaimLogs = new    List<ClaimHistoryModel.ClientLog>();
using (SidDbContext db = new SidDbContext())
    {
        var oldClaim = db.Client.FirstOrDefault(x => x.ClaimId == clientModel.ClaimId);

        oldClaim.CellNumber = clientModel.CellNumber;

        ClaimLogs = GetClaimLog(db); //here am getting a method which update the Logtbl.(all the changes will be saved) no error on this funstion
      //it works if i save changes here but it won't update the history table which shows the fieds before and after.            
       db.claimAudit.AddRange(ClaimLogs ); Error here
       db.SaveChanges();
    }

Am getting this error:

Cannot convert from System.Collections.Generic.List<ClaimHistoryModel.ClientLog> to System.Collections.Generic.IEnumerable<claimAudit>

Berin Loritsch
  • 11,400
  • 4
  • 30
  • 57
Mpho
  • 13
  • 2
  • 11

1 Answers1

0

That will never work. You cannot add a ClientLog collection to a collection that expects ClaimAudit.That's the same as trying to add an object of type Order to a collection of objects of type User.I hope I'm making sense...

The example below will work because db.claimAudit.AddRange() expects two things:

  1. An IEnumerable
  2. An IEnumerable of type ClaimAudit

And List<ClaimAudit> satisfies both those conditions.As soon as you change List<ClaimAudit> to List<ClientLog> you are violating point 2 and the code will break

var claimAudits = new List<ClaimAudit>();
var claim1 = new ClaimAudit();
var claim2 = new ClaimAudit();
claimAudits.Add(claim1);
claimAudits.Add(claim2);

db.claimAudit.AddRange(claimAudits);

Maybe you actually need to insert claim logs instead of claim audits, if that's the case then just change this line:

db.claimAudit.AddRange(ClaimLogs);

to this:

db.claimLogs.AddRange(ClaimLogs);
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120