Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 918429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:22:25+00:00 2026-05-15T18:22:25+00:00

I have an Invoice entity and it has child InvoiceLog entities. When I first

  • 0

I have an Invoice entity and it has child InvoiceLog entities. When I first create an Invoice and add its InvoiceLog entities and save, it works fine. However, if I then edit the Invoice and try to add additional InvoiceLog entities, it completely ignores the new InvoiceLog entities and doesn’t save them at all. Any ideas what I’m doing wrong?

//POST: /Secure/Invoices/Save/
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(Invoice invoice)
        {
            invoice.UpdateDate = DateTime.Now;
            invoice.DeveloperID = Developer.DeveloperID;
            invoice.InvoiceStatusID = (int)Enums.InvoiceStatus.Open;
            if (invoice.InvoiceID == 0)
            {//inserting new invoice.
                DataContext.InvoiceData.Insert(invoice);
            }
            else
            {//attaching existing invoice.
                DataContext.InvoiceData.Attach(invoice);
            }
            AddHours(invoice);
            //save changes.
            DataContext.SaveChanges();
            //redirect to invoice list.
            return RedirectToAction("Index");
        }

        private void AddHours(Invoice invoice)
        {
            //get existing logs.
            IQueryable<InvoiceLog> existingLogs = null;
            if(invoice.InvoiceID > 0)
            {
                existingLogs = DataContext.InvoiceData.GetLogs(invoice.InvoiceID);
            }
            //create new logs.
            var numDays = invoice.EndDate.Subtract(invoice.StartDate).TotalDays;
            for (int k = 0; k <= numDays; k++)
            {
                //check if log already exists.
                var existingLog = existingLogs.ToList().FindIndex(l => l.LogDate == invoice.StartDate.AddDays(k));
                if (existingLog == -1)
                {
                    //add new log.
                    var log = new InvoiceLog();
                    log.CreateDate = DateTime.Now;
                    log.UpdateDate = DateTime.Now;
                    log.Hours = 0;
                    log.InvoiceID = invoice.InvoiceID;
                    log.LogDate = invoice.StartDate.AddDays(k);
                    invoice.InvoiceLogs.Add(log);
                }
            }
        }

Thanks,
Justin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:22:26+00:00Added an answer on May 15, 2026 at 6:22 pm

    You can try to add InvoiceLogs items before attaching the invoice:

        else
        {//attaching existing invoice.
            AddHours(invoice);
            DataContext.InvoiceData.Attach(invoice);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume we have an Invoice entity which has many InvoiceDetails . Each invoice detail
I have Invoice and Supplier hibernate entities. One Invoice has only one supplier. One
I'm using .NET 4 EF and am trying to save the child entities (InvoiceLogs)
I have mapped a simple entity, let's say an invoice using Fluent NHibernate, everything
I have two entity classes: public class Invoice { public int ID { get;
I have an invoice model, that has many invoice items. I have a form
I have a Client Entity and it has a to many relationship (entries) with
I have an entity in Java and I would like Hibernate to create a
I have an invoice form. This is a simplified version: so it has line
I have an existing application that has the following entities in the database Customer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.