有没有办法为 EF 模型自动创建 CRUD(目前是 DB First)

Is there a way to automatically create CRUD for EF Model (DB First currently)

我正在创建一个 WPF 应用程序,并且我有一个现有的数据库,我想使用它而不是重新创建它。如果必须的话,我会的,但我宁愿不这样做。数据库是 sqlite,当我稍后将它添加到我的数据并基于数据库创建数据模型时,我得到了模型和数据库上下文,但是没有为 CRUD 或例如 .ToList() 创建的方法,所以我可以 return table.

上的所有项目

我是否需要手动创建所有这些,或者是否有一种方法可以像 MVC 脚手架那样创建?

我正在使用安装了 Nu-Get 的 VS 2017、WPF、EF6 和 Sqlite

Entity Framework 是对象关系映射器

这意味着它将把您的 C# 对象映射到表。

每当您从 bd 创建模型时,它都会创建一个 Context Class,它将继承 DbContext。在此 class 中,您将找到 DbSet<Tablename> Tablename{get; set;} 中的所有表格。基本上,此列表包含行。在此列表上执行的操作将影响 SaveChange 方法上的数据库。

Example for CURD

    public DbSet<Student> Students { get; set; }
    //Create
        using (var context = new YourDataContext()) {

          var std = new Student()
           {
              Name = "Aviansh"
            };

             context.Students.Add(std);
             context.SaveChanges();
                    }//Basically saving it will add a row in student table with name field as avinash 

//Delete

     using (var context = new YourDataContext()) {

        var CurrentStudent=context.Students.FirstOrDefault(x=>x.Name=="Avinash")
        CurrentStudent.context.Students.Remove(CurrentStudent);
        context.SaveChanges();
}

注意:在 SaveChanges 上,更改将反映在 Db

回答标题中的问题

没有

没有 click-a-button 搭建 UI 脚手架的方法,就像您使用 MVC 获得的那样。

如果您一次只处理一个 table,那么您可以构建一个通用存储库,该存储库 return 是给定 table 的列表。这不会为您节省太多编码,但您可以做到。

如果您使 return 成为 iQueryable 而不仅仅是一个列表,那么您就可以 "chain" 这样的查询。 Linq 查询不会变成 SQL 直到你强制迭代并且你可以基于另一个添加标准,select 等等等等以获得灵活性。

在您的 post 的 body 中,您询问了读取和写入数据的方法。这似乎与另一个问题几乎完全无关,因为它是数据访问而不是 UI.

"there are no methods created for CRUD or for instance .ToList() so I can return all of the items on the table."

有 LINQ 扩展方法形式的可用方法。 ToList() 是其中之一,除了通常使用 async await 和 ToListAsync。

其中和Select为其他扩展方式

尽管如此,您还是会编写任何暴露这些结果的模型层。

我不清楚您是不知道 linq 还是什么,但这里有一个示例查询。

        var customers = await (from c in db.Customers
                               orderby c.CustomerName
                               select c)
                               .Include(x => x.Orders) //.Include("Orders") alternate syntax
                               .ToListAsync();

EF 使用 "lazy loading" 相关实体,Include 使其读取每个客户的订单。