C#/VB.net 中数据库实体和对象之间的抽象
我刚刚开始使用 Visual Studio 进行开发(尽管这可以被视为一个更通用的语言问题),并且需要完成有关如何将数据库实体/表与代码中的类相关联的设计答案。
目前,我创建一个类,其属性或 iVar 直接对应于实体中的字段。
例如,当我要从表中获取所有“客户”时,我对数据库运行 Select all 语句,并且对于返回的每个结果行,我实例化一个新类对象,其中使用类的默认构造函数来设置实例变量。 (我真的希望有一个像 PHP 的 mysql-fetch-object() 这样的方法?)
然后我可以添加每个对象来表示一个 List(of Customer) 并返回它。
这似乎不是最好的方法。如果我要更改数据库架构,我也必须在类中反映这些更改。
非常感谢。
I've just gotten started with development using Visual Studio (though this can be seen as a more language-generic question), and need done design answers regarding how to suitable relate a Database Entity/Table with an Class in-code.
Currently, I create a class who's attributes or iVars directly correspond to the fields in an Entity.
When I come to fetching all 'Customers from a Table' for example, I run a Select all statement against the database and for each result-row returned, I instantiate a new class Object where I use the Class' default constructor to set the instance variables. (I really wish there was a method like PHP's mysql-fetch-object()?)
I can then add each of these objects to say, a List(of Customer) and return it.
This doesn't seem like the best way to do it. If I were to change the Database schema, I'd have to reflect those changes in the class also.
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您正在寻找的是“对象关系映射器”,例如 Entity Framework 或 NHibernate。
Entity Framework 是 Microsoft 的解决方案,它是 .NET 框架的一部分(尽管对于最新版本 (4.1),您需要单独下载)。 NHibernate 是一个第三方库。两者都应该能够帮助您完成要做的事情。
链接:
It sounds like what you're looking for is an "object-relational mapper", such as Entity Framework or NHibernate.
Entity Framework is Microsoft's solution, and it's part of the .NET framework (though for the latest version (4.1), you'll need to download that separately). NHibernate is a third-party library. Both should be able to help with what you're trying to do.
Links: