Write List <> to the database asp.net

Related searches
  • I'm stuck writing to the database

  • Don't throw me any mistakes

  • Write commands do not work

controller:

[HttpPost]
public async Task<ActionResult>PartialTabelaEcp(string userDate)
{
     var numerMiesiaca = 1;
     var numerRoku = 1;
     var dbExists = _ecpContext.Karta.FirstOrDefault(f => f.DzMiesiaca == 1 && f.Miesiac == 
                             numerMiesiaca && f.Rok == numerRoku && f.Login == userName);
    if (dbExists == null)
    {
        List<Karta_Model> objKartaModel = new List<Karta_Model>();

        for (int i = 1; i <= liczbaDni; i++)
        {
            DateTime thisDate = new DateTime(numerRoku, numerMiesiaca, i);

            var day = culture.DateTimeFormat.GetDayName(thisDate.DayOfWeek);
            var model = new Karta_Model()
            {
                DzMiesiaca = i,
                DzTygodnia = day,
                Rozpoczecie = "00:00"
            };

            objKartaModel.Add(model);
        }

        await _ecpContext.Karta.AddRangeAsync(objKartaModel);
        await _ecpContext.SaveChangesAsync();
    }
}

return PartialView("_TabelaEwidencja" );
  • in _ecpContext.Karta.AddRangeAsync(objKartaModel), in objKartaModel I have a list of models, as many as 'i'

model:

public partial class Karta_Model
{
    public int? DzMiesiaca { get; set; }
    public string? DzTygodnia { get; set; }
    public string? Rozpoczecie { get; set; } 
}

Your method is invoking async operations:

_ecpContext.Karta.AddRangeAsync(objKartaModel);
_ecpContext.SaveChangesAsync();

So you need to await them:

await _ecpContext.Karta.AddRangeAsync(objKartaModel);
await _ecpContext.SaveChangesAsync();

To do so, your method also needs to be async:

public async Task<ActionResult> PartialTabelaEcp(string userDate)

If you don't await asynchronous operations then the code proceeds before they've been completed, so when you get to your SaveChangesAsync there's nothing to save. (And that operation itself may fail and you're not observing the failure.)

Reading and Writing Lists to a File in Python, Using the read and write Methods. To deal with characters (strings) the basic methods work excellent. Saving such a list line by line into the file listfile.txt� Arguments z . A list object to be written. Each item in the list is perferably a data frame. If not, it is converted into a data frame. All the contents are coerced into characters to avoid loss of information (e.g., a loss of zero in 5.130.

As @ADyson mentioned you're using async methods in sync code. So should either need to make your code async (as did @David or @Amankhani MohammadJavad) or make your methods sync. EF gives you both ways of AddRange and SaveChanges.

Change

 _ecpContext.Karta.AddRangeAsync(objKartaModel);
 _ecpContext.SaveChangesAsync();

To

 _ecpContext.Karta.AddRange(objKartaModel);
 _ecpContext.SaveChanges();

erer source: R/write.list.r, The truth about list posts. Don’t write a list post because you think it gets more shares. Don’t write a list post because you think it’ll be quick and easy. Don’t write one when you’re bored with lists. Because you’ll surely bore your readers to tears. Write a list post when it’s the best format for sharing your tips with your

You are using an async method but your method is not an async method You can do this:

  [HttpPost]
public async Task<ActionResult> PartialTabelaEcp(string userDate)
        {
             var numerMiesiaca = 1;
             var numerRoku = 1;
             var dbExists = _ecpContext.Karta.FirstOrDefaultAsync(f => f.DzMiesiaca == 1 && f.Miesiac == 
                                           numerMiesiaca && f.Rok == numerRoku && f.Login == userName);
            if (dbExists == null)
            {
                List<Karta_Model> objKartaModel = new List<Karta_Model>();

                for (int i = 1; i <= liczbaDni; i++)
                {
                    DateTime thisDate = new DateTime(numerRoku, numerMiesiaca, i);


                    var day = culture.DateTimeFormat.GetDayName(thisDate.DayOfWeek);
                    var model = new Karta_Model()
                    {
                        DzMiesiaca = i,
                        DzTygodnia = day,
                        Rozpoczecie = "00:00"
                    };

                    objKartaModel.Add(model);

                }


                await _ecpContext.Karta.AddRangeAsync(objKartaModel);
                await _ecpContext.SaveChangesAsync();
            }

        }
 return PartialView("_TabelaEwidencja" );

In other words, writing things down on a to-do list means that you stop worrying, and actually have time to prioritise and then complete your tasks. But this handy brain-trick is also the reason why some to-do lists don’t work: they are too long , or too hard, to complete , and we start worrying again.

This logic will first convert the items in list to string(str).Sometimes the list contains a tuple like . alist = [(i12,tiger), (113,lion)] This logic will write to file each tuple in a new line.

A resume reference list is a document that provides contact and background information on professional references. Recruiters and hiring managers may contact people on your reference list during the hiring process to learn more about your professional history, job performance and other details about the kind of employee you may be if hired.

Whether virtual, in-person, or hybrid - WriteAhead is here to ensure your student excels.

Comments
  • you're using async methods but not waiting for the results before moving to the next command.
  • @ADyson ,and how could I do this programatically?
  • Seems you maybe need to study some introductory async programming tutorials - both to understand what async can be used for, and also how to use it. tldr: Either make your outer method async and then await the methods, or use the synchronous versions of those methods instead. using the async/await pattern is generally advantageous in this situation because it frees up the thread in your .NET code while the database operation is executing, meaning your server has more capacity to respond to other requests.
  • ...because? Don't just dump code on someone. Explain what your change means and why it works (and any implications such as, in this case, loss of the advantages of asynchronous I/O operations.) Otherwise people who don't really understand this topic might just copy and paste your code blindly without understanding it. And you wouldn't want that
  • You've explined :)
  • I've explained it partially. But still you should add it more fully to the answer body if you want to avoid downvotes...
  • You forgot to await the async calls inside the method. If you don't do that then you won't fix the issue.
  • You can't say this is a flawed solution, it can be solved this way Where's the problem with this?
  • Sorry, at the time I wrote that my page hadn't updated with your edit where you added the await. I have deleted my second comment now. However I do think you should also add text to the answer explaining why await is also a necessary part of the solution, as other answers have done.
  • I wanted to get the job done quickly and solve their mine Thank you