繁体   English   中英

C# CRUD 问题 - UPDATE 面板无法正常工作

[英]C# CRUD problem - The UPDATE panel does not work as I expect

我尝试制作一个支持完整 CRUD 的程序,但是...更新部分不起作用

   using System;
        using System.Collections.Generic;
        using System.Text;

        namespace MyLibary.Models
        {
            class UpdateBooks
            {
                public void updateCompBook()
                {
                    using (var bookUp = new BookContext())
                    {
                        // This is a full CRUD program that saves books in a databases
                        // I have some problems with the Update part..
                        ShowFutureBooks bookShow = new ShowFutureBooks(); 
                        bookShow.FutureBooks(); // shows all three books

                        Console.WriteLine("Type the id of the book you want to update"); // currently there is 3 books saved in the database

                        int id = Int32.Parse(Console.ReadLine()); // lets say i that i choose the if 1

                        // what i would like to do and cannot, is that get only the book that has id 1 so that i can update it


                        Console.WriteLine("Update the book according to following: BookName, BookAuthor, BookSummary");



                        BooksToRead bU = new BooksToRead()
                        {
                            BookName = Console.ReadLine()
                          , BookAuthor = Console.ReadLine()
                          , BookSumary = Console.ReadLine()
                        }; 
                        bookUp.BooksToRead.Add(bU);
                        Console.WriteLine("Press enter to save changes.");
                        Console.ReadLine();
                        bookUp.SaveChanges();
                        Console.WriteLine("The bok has been updated!");

                    }
                }
            }
        }

我想从数据库中获取一本书 id,然后更新那本特定的书,但它不起作用。

这段代码

    BooksToRead bU = new BooksToRead()
    {
        BookName = Console.ReadLine()
        , BookAuthor = Console.ReadLine()
        , BookSumary = Console.ReadLine()
    }; 
    bookUp.BooksToRead.Add(bU);

创建一个新的 BooksToRead 对象,然后将其添加到列表中。 这实际上将执行插入而不是更新。 您必须遍历您的列表并找到带有插入 id 的 BooksToRead 对象,然后在保存之前更改其值。

当你想更新时,你应该首先加载你想要编辑的书

var bookToEdit = bookUp.BooksToRead.Find( id );

现在修改你喜欢的属性

bookToEdit.BookName = Console.ReadLine();
...

完成编辑后,保存更改

bookUp.SaveChanges();

当向您的应用程序添加 crud 时,我建议将其放在一个类中。 然后这个类将以名称repository结束(查找它你会发现很多例子)。

public class BookRepository
{
    /// <summary>
    /// This method will update the entity by using the bookId.
    /// </summary>
    /// <param name="bookId">The is of the book</param>
    /// <param name="name">The book Name (to be updated)</param>
    /// <param name="author">The book author (to be updated)</param>
    /// <param name="summary">The book summary (to be updated)</param>
    public void Update(int bookId, string name, string author, string summary)
    {
        var _context = new BookContext();
        var myBook = _context.Books.First(book => g.bookId == bookId);//finds the first entity with the given bookId
        myBook.name = name;//this for all variables
        _context.SaveChanges();//save the changes to the database.
    }
}

调用方法将类似于: new BookRepository.Update(0, "", "", ""); .

请注意,以上只是一个简单的例子。 需要进行一些更改才能使其适用于您的特定数据模型。 请参阅以下链接以了解有关存储库模式的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM