用于 C# 的简单 CRUD 生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/672020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:45:01  来源:igfitidea点击:

Simple CRUD Generator for C#

c#databasecode-generationcruddao

提问by Martin

I am looking for a simple CRUD (or DAL) Generator for C#. I don't want anything heavyweight since I only have a couple of tables in a SQL Server 2008 database.

我正在为 C# 寻找一个简单的 CRUD(或 DAL)生成器。我不想要任何重量级的东西,因为我在 SQL Server 2008 数据库中只有几个表。

Any suggestions? I know .netTiers, but it is way too much for what I need.

有什么建议?我知道 .netTiers,但它对于我需要的东西来说太多了。

Thanks!

谢谢!

UPDATE: I tried LINQ to SQL and it doesn't work well for my needs.

更新:我尝试了 LINQ to SQL,但它不能很好地满足我的需求。

回答by Paul Mendoza

LINQ To SQL is easy and it's built into .NET 3.5 SP1.

LINQ To SQL 很简单,它内置于 .NET 3.5 SP1 中。

回答by MrTelly

回答by John Saunders

What about using a DataSet created using the DataSet designer. I remember back in the Old Days (.NET 1.0 and 1.1), we would drag a DataAdapter onto the design surface, specify the Select query, and the Insert, Update and Delete queries would be created for us, based on the Select query.

使用 DataSet 设计器创建的 DataSet 怎么样。我记得在过去(.NET 1.0 和 1.1),我们将一个 DataAdapter 拖到设计图面上,指定 Select 查询,然后会根据 Select 查询为我们创建插入、更新和删除查询。

回答by jason saldo

The Visual Studio wizards will create a simple CRUD app for you.

Visual Studio 向导将为您创建一个简单的 CRUD 应用程序。

Drag a datagrid on your form, click the connection property and follow the wizards from there.

在表单上拖动数据网格,单击连接属性并从那里按照向导进行操作。

Not best practices but is simple and works...

不是最佳实践,但很简单且有效......

回答by John Rasch

I have used SubSonic on past projects, it's lightweight and easy to use.

我在过去的项目中使用过 SubSonic,它轻巧且易于使用。

They offer a simple tutorial videoand it should take no more than 10 minutes to get it completely setup. I recommend watching the second half of the video that deals with Web Application Projects because it shows you how to create a customized Visual Studio button that creates the DAL for you whenever you click on it instead of using a custom build-provider as they suggest in first half of the video.

他们提供了一个简单的教程视频,完全设置它应该不会超过 10 分钟。我建议观看处理 Web 应用程序项目的视频的后半部分,因为它向您展示了如何创建自定义的 Visual Studio 按钮,该按钮在您单击时为您创建 DAL,而不是像他们建议的那样使用自定义构建提供程序视频的前半部分。

It offers several ways to access your data, Active Record, generating typed stored-procedures and views, or a query languagethat you can use.

它提供了多种方法来访问您的数据、Active Record、生成类型化存储过程和视图或您可以使用的查询语言

After using it, I have found a few quirks:

使用后,我发现了一些怪癖:

  • If you use a generated stored-procedure that does not have a parameter, it will throw a NullReferenceException. A workaround is to create a dummy parameter that isn't used in the procedure
  • The DeepSave()function does not work in the current 2.1 version, you'll have to individually save data from joined tables
  • When you use a coditional (e.g. Where(Tag.Columns.TagName).IsEqualTo("subjective"), make sure you use the string value Tag.Columns.TagNameto reference the column - otherwise an exception will be thrown if you try to use the Column.Schema
  • 如果您使用没有参数的生成存储过程,它将抛出一个NullReferenceException. 一种解决方法是创建一个未在过程中使用的虚拟参数
  • DeepSave()功能在当前 2.1 版本中不起作用,您必须单独保存连接表中的数据
  • 当您使用 coditional 时(例如Where(Tag.Columns.TagName).IsEqualTo("subjective"),确保使用字符串值Tag.Columns.TagName来引用列 - 否则,如果您尝试使用Column.Schema

回答by Damovisa

Just in case money isn't a big concern, I've had great success with templates in CodeSmith.

以防万一钱不是大问题,我在CodeSmith 中使用模板取得了巨大成功。

There are plenty of sample templates out there for CRUD operations and it'll read directly from your database.

有很多用于 CRUD 操作的示例模板,它会直接从您的数据库中读取。

回答by Giovanni Galbo

Visual Studio comes with a code generator that hardly anyone knows about called T4.

Visual Studio 带有一个几乎没有人知道的代码生成器,称为T4

You should be able to use it relatively easily to create CRUD templates.

您应该能够相对轻松地使用它来创建 CRUD 模板。

EDIT

编辑

And here's an example how: http://www.olegsych.com/2008/01/how-to-use-t4-to-generate-crud-stored-procedures/

这是一个例子:http: //www.olegsych.com/2008/01/how-to-use-t4-to-generate-crud-stored-procedures/

回答by bendewey

Have you tried the Dynamic Data Web Application (uses LINQ to SQL) or the Dynamic Data Entities Web Application (uses Entity Framework).

您是否尝试过动态数据 Web 应用程序(使用 LINQ to SQL)或动态数据实体 Web 应用程序(使用实体框架)。

MSDN: Walkthrough: Creating a New ASP.NET Dynamic Data Web Site Using Scaffolding

MSDN:演练:使用脚手架创建新的 ASP.NET 动态数据网站

回答by Bill Martin

Take a look at Codesmith. The 2.6 versionis free and the later versions are for purchase.

看看Codesmith。在2.6版本是免费的,后来的版本的报价。

回答by tbone

Check out LLBLGen Pro, I swear by it. It is not free, but not expensive. You can get up and running (writing code) within an hour, and the templates it comes with generate code that can do basically anything you would want. It is so nice (and productive) to not even have to think about database interface code anymore.

看看 LLBLGen Pro,我发誓。它不是免费的,但也不贵。您可以在一小时内启动并运行(编写代码),它附带的模板生成的代码基本上可以做您想做的任何事情。甚至不必再考虑数据库接口代码,这真是太好了(而且富有成效)。