locked
Blazor Server WebAPI EFCore5.X - HTTP POST all or nothing RRS feed

  • Question

  • User379720387 posted

    I am at the point where I need write a large chunk of data to the db. This chunk consists of 8 Entities.

    In EF6.4 in my old app I would create new instances and then say SaveChanges. If one instance failed the whole thing failed (do or die).

    At the moment I am not seeing how I can achieve the same with EF Core and Web API, wouldn't I have 8 different HTTP POST actions?

    What is the technique for doing this?

    My NewTxnModel looks like this:

    public int TransactionId { get; set; }
            public string CName { get; set; }
            public string LName { get; set; }
    
            public DateTime TDate { get; set; }
            public string BDate { get; set; }
            public string Initials { get; set; }
            public bool IsBilled { get; set; }
            public bool IsPaid { get; set; }
            public string SvcLevel { get; set; }
    
            public bool IsAdjusted { get; set; }
            public decimal adjAddOn { get; set; }
            public decimal Tax { get; set; }
            public decimal ccAddOn { get; set; }
            public decimal Charge { get; set; }
            public decimal PayAmount { get; set; }
    
            public string CurrencySymbol { get; set; }
            
            public List<ServiceItem> ServiceItems { get; set; }
            public List<ShoeItem> ShoeItems { get; set; }
            public List<FileItem> FileItems { get; set; }
            public List<ImageItem> ImageItems { get; set; }
            public List<NoteItem> NoteItems { get; set; }
            public List<VoiceItem> VoiceItems { get; set; }

    Wednesday, April 7, 2021 2:08 PM

Answers

  • User475983607 posted

    At the moment I am not seeing how I can achieve the same with EF Core and Web API, wouldn't I have 8 different HTTP POST actions?

    What is the technique for doing this?

    There's nothing stopping your from saving multiple entities in one shot. 

    https://www.learnentityframeworkcore.com/dbcontext/adding-data

    https://docs.microsoft.com/en-us/ef/core/saving/related-data

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 7, 2021 2:17 PM
  • User1686398519 posted

    Hi wavemaster, 

    In EF6.4 in my old app I would create new instances and then say SaveChanges. If one instance failed the whole thing failed (do or die).

    At the moment I am not seeing how I can achieve the same with EF Core and Web API, wouldn't I have 8 different HTTP POST actions?

    If any of these entities are not successfully saved to the database, will all operations not be applied to the database?

    In this case, you can use transactions.

    • By default, if the database provider supports transactions, all changes in a single call to SaveChanges are applied in a transaction.
    • If any of the changes fail, then the transaction is rolled back and none of the changes are applied to the database.
    • This means that SaveChanges is guaranteed to either completely succeed, or leave the database unmodified if an error occurs.

    Using Transactions

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 8, 2021 6:47 AM

All replies

  • User475983607 posted

    At the moment I am not seeing how I can achieve the same with EF Core and Web API, wouldn't I have 8 different HTTP POST actions?

    What is the technique for doing this?

    There's nothing stopping your from saving multiple entities in one shot. 

    https://www.learnentityframeworkcore.com/dbcontext/adding-data

    https://docs.microsoft.com/en-us/ef/core/saving/related-data

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 7, 2021 2:17 PM
  • User1120430333 posted

    You could send all the objects/data at onetime in a single container object doing a single post and persist the data from the container object. 

    Wednesday, April 7, 2021 2:46 PM
  • User379720387 posted

    @mgebhard thanks for the link to learnentityframeworkcore, that got my going

    @DA924 currently I am as far as Serializing the new transaction into json, unfortunately I now have "object cycle detected"

    Once I have that fixed I will circle back on the persisting of the data, no yet clear where that fits in.

    Thursday, April 8, 2021 2:15 AM
  • User1686398519 posted

    Hi wavemaster, 

    In EF6.4 in my old app I would create new instances and then say SaveChanges. If one instance failed the whole thing failed (do or die).

    At the moment I am not seeing how I can achieve the same with EF Core and Web API, wouldn't I have 8 different HTTP POST actions?

    If any of these entities are not successfully saved to the database, will all operations not be applied to the database?

    In this case, you can use transactions.

    • By default, if the database provider supports transactions, all changes in a single call to SaveChanges are applied in a transaction.
    • If any of the changes fail, then the transaction is rolled back and none of the changes are applied to the database.
    • This means that SaveChanges is guaranteed to either completely succeed, or leave the database unmodified if an error occurs.

    Using Transactions

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 8, 2021 6:47 AM