locked
How do I populate a database with a NO async method? RRS feed

  • Question

  • User483634116 posted

    I have the following Model with the following code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace MyApp.Models
    {
        public class AnModel
        {
            [Key]
            public int Id { get; set; }
            [Required]
            public int Idtwo { get; set; }
            public DateTime Date { get; set; }
            public int IdUser { get; set; }
            public string State { get; set; }
            public TimeSpan? HoraOne { get; set; }
            public TimeSpan? HoraTwo { get; set; }
        }
      }

    When I press a button in the view, the following method is called in the Controller:

     [HttpPost]
            public IActionResult Create(List<int> st)   
            {
                foreach (int value in st)
                {
                 /*here must go the code which is missing*/ 
                }
                return View("Home");
            }

    What code should I write in the method, to be able to enter manually in the menu X data to the database?

    I know this is usually done with something like "public async Task <IActionResult> Create ([Bind (" id, ... " and there is later a "_context.Add (NewEntity);" where "_context" is a "private readonly App-insertsystemname-DbContext _context;" etc. What I am asking, is how should I write the code, to enter data I want manually, in a NO async method, since I really need to handle the code this way.


    Wednesday, December 9, 2020 8:36 PM

Answers

  • User1686398519 posted

    Hi roxylalondef,  

    roxylalondef

    be able to enter manually in the menu X data to the database

    roxylalondef

    to enter data I want manually, in a NO async method,

    Do you need to store list-type data in the database?

    If so, you can refer to the example I gave. If this is not the case, you can describe your needs in more detail so that we can better assist you.

    Controller

        public class Test88Controller : Controller
        {
            private readonly DailyCoreMVCDemoContext _context;
            public Test88Controller(DailyCoreMVCDemoContext context)
            {
                _context = context;
            }
            public IActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public IActionResult Create(List<int> st)
            {
                foreach (int value in st)
                {
                    _context.AnModels.Add(new AnModel {IdUser=value });
                }
                _context.SaveChanges();
                return View("Index");
            }
        }

    Index

    <form method="post" action="/Test88/Create">
        @for (int i = 0; i < 3; i++)
        {
            <input name="st" />
        }
        <button type="submit">submit</button>
    </form>

    Here  is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 10, 2020 2:51 AM

All replies

  • User475983607 posted

    What I am asking, is how should I write the code, to enter data I want manually, in a NO async method, since I really need to handle the code this way.

    Use SaveChanges() rather than SaveChangesAsync().

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.savechanges?view=efcore-5.0

    Wednesday, December 9, 2020 8:46 PM
  • User1686398519 posted

    Hi roxylalondef,  

    roxylalondef

    be able to enter manually in the menu X data to the database

    roxylalondef

    to enter data I want manually, in a NO async method,

    Do you need to store list-type data in the database?

    If so, you can refer to the example I gave. If this is not the case, you can describe your needs in more detail so that we can better assist you.

    Controller

        public class Test88Controller : Controller
        {
            private readonly DailyCoreMVCDemoContext _context;
            public Test88Controller(DailyCoreMVCDemoContext context)
            {
                _context = context;
            }
            public IActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public IActionResult Create(List<int> st)
            {
                foreach (int value in st)
                {
                    _context.AnModels.Add(new AnModel {IdUser=value });
                }
                _context.SaveChanges();
                return View("Index");
            }
        }

    Index

    <form method="post" action="/Test88/Create">
        @for (int i = 0; i < 3; i++)
        {
            <input name="st" />
        }
        <button type="submit">submit</button>
    </form>

    Here  is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 10, 2020 2:51 AM
  • User-474980206 posted

    Note that this will cause scaling issues with asp.net core. If you need to make sync database or network calls, you should use a async thread pool to execute them.

    Thursday, December 10, 2020 3:50 PM
  • User483634116 posted

    Hi YihuiSun,

    Thank you so much! It was your code that allowed me to solve this problem.

    Sorry for the late reply, I was busy with a lot of real life problems.

    Best Regards,

    roxylalondef

    Monday, December 21, 2020 3:25 PM