从代码中创建c#中的类 [英] Create class in c# from code
问题描述
你好
我有一个想法,我不知道开始做什么和搜索怎么样。
当我使用实体时在项目中的框架工作模型,我应该为每个表创建类,有一些修改数据库的方法,如update-delete-Insert-select。
我的想法是创建ASP.NET中的类库或其他类,当执行它时,从数据库模型中查看所有表,因此当我选择一个表并单击运行按钮时,它会生成具有名称和特定位置的类和方法。
例如:table1(id,name,family)class = table1class location = / myfolder methods = save,update,delete。
现在,有什么解决方案吗?我该搜索什么?怎么开始呢?
提前谢谢。
我尝试了什么:
创建一个类库,为实体框架work.classes中的表生成类以修改数据库。
Hello
I have an idea and i dont know what begin to do it and what about search.
When I use entity frame work model in project,i should create classes for each table,that have some method to modify database like update-delete-Insert-select.
My idea is create a classlibrary or other in ASP.NET, that when execute it, view all tables from database model,so when I choose a table and click on run button,then class and methods of it generated with name and specific location.
For example:table1(id,name,family) class=table1class location=/myfolder methods=save,update,delete.
Now, is there any solution about it? What do I search? How begin it?
Thank you in advance.
What I have tried:
Create a classlibrary to generate class for tables in entity frame work.classes to modify database.
推荐答案
听起来就像你使用 T4 [ ^ ](文本模板转换工具包)生成您的代码。基本上,您将从您的功能所需的代码模板开始,然后您将打开与数据库的连接以读取表结构 - 根据您使用的数据库,有不同的方法来执行此操作,最后,您将此信息应用于您的模板。从我链接的页面开始,然后从那里向外工作。
Sounds like what you are after is using T4[^] (Text Template Transformation Toolkit) to generate your code. Basically, you will start with a template of the code you need for your functionality, then you'll open up a connection to the database to read the table structures in - there are different ways of doing this depending on the database you are using, and finally you will apply this information to your template. Start with the page I linked to and then work outwards from there.
遵循某种存储库模式。从包含所有这些常用方法的通用基类扩展实体框架和业务之间的存储库。
以下是此类接口的示例。您还需要编写具体的通用方法。
Follow some sort of repository pattern. Have a repository in between of your entity framework and business extended from a generic base class containing all those common methods.
Following is an example of such interface. You need to write the concrete generic methods as well.
public interface IRepository<T> where T : class
{
Task<T> Create(T entity);
T Read(Guid Id);
T Update(T entity);
T Delete(Guid Id);
}
看看这样的东西让你入门:
实体框架简介 [ ^ ]
Have a look at something like this to get you started:
Introduction to the Entity Framework[^]
这篇关于从代码中创建c#中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!