1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

EF Core updating record causes an Id of 0

Discussão em 'Angular' iniciado por TheIronCheek, Maio 23, 2025.

  1. TheIronCheek

    TheIronCheek Guest

    I'm extremely new to Entity Framework and I have a project I inherited that requires a record to be updated. I have the following code:

    foreach(Invoice item in paidInvoices) {
    item.Paid = true;
    item.PaidTsp = DateTime.Now;

    _context.Entry(item).State = EntityState.Modified;
    }

    _context.SaveChanges();


    If I do a SELECT directly in the db to view the contents of the Invoice table, the records are updating correctly.

    On another page of the site, though, I have a table of Invoice records and these updated records display but they're missing information. For example, in the below snippit, entry.Hours is just 0 instead of what it should be.

    <table class="table table-hover">
    <thead>
    <tr>
    <th>Status</th>
    <th>Date</th>
    <th>Hours</th>
    </tr>
    </thead>
    <tbody>
    <tr *ngIf="searching"><td colspan="9" align="center"><h4>Loading results</h4></td></tr>
    <tr *ngIf="!searching && totalCount == 0"><td colspan="9" align="center"><h4>Nothing found</h4></td></tr>
    <ng-template ngFor let-entry [ngForOf]="results" let-i="index" *ngIf="!searching">
    <tr>
    <td (click)="open(entry)" role="button">
    <span [ngClass]="getStatusStyle(entry.Status)">{{entry.Status}}</span>
    </td>
    <td (click)="open(entry)" role="button">{{entry.Date | date: 'MM/dd/yyyy'}}</td>
    <td (click)="open(entry)" role="button">{{entry.TotalHours}}</td>
    </tr>
    </ng-template>
    </tbody>
    </table>


    When I click the records, open(entry) runs:

    public open(t: invoice) {
    if (t.Id == 0) {
    this.InvoiceService.insert(t)
    .subscribe((result) => {
    this.router.navigate([`/invoice/${result.Id}`]);
    });
    }
    else {
    this.router.navigate([`/invoice/${t.Id}`]);
    }
    }


    There must be a 0 being returned for the Id somewhere because when open(entry) runs, a new record is created.

    Continue reading...

Compartilhe esta Página