如何在 MVC 3 中处理动态数据模型
在我当前正在开发的应用程序中,有一部分我不知道数据库架构,因为它是由用户定义的,并且表是动态生成的。
正在做的应用程序是ASP.Net MVC 3和SQL Server 2008 R2。对于这样的应用程序,您建议对数据对象和数据访问层使用什么。
一种选择是简单地从 DAL 返回数据表并使用它们。但它们是无类型的。 该选项是从结果集中动态创建对象。但它的表现不会很好。
还有其他办法吗?实体框架的 Code First 可能有帮助吗? Expando 对象怎么样?
非常重要的一件事是应用程序的数据量非常大,例如 GB 的数据。
In the application I am currently working on, there is a portion in which I do not know the db schema as it is defined by the users and tables are generated dynamically.
The application is being done is ASP.Net MVC 3 and SQL Server 2008 R2. For such an application what would you suggest to use for data objects and data access layer.
One option is to simply return datatables from DAL and use them. but they would be untyped.
The option would be to create objects dynamically from resultset. but it would not perform very well.
Is there any other way? Entity Framework's Code First could be helpful? What about Expando objects?
One thing very important is that the application is very data extensive, something like GBs of data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由用户动态创建表的应用程序不是实体框架的场景。实体框架需要映射到表的已编译类(如果未按约定推断则需要映射),并且您的代码必须使用这些类才能利用它们。
在我看来,动态创建表的应用程序不能提供强类型访问或出色的性能。这两个要求都期望您针对定义良好的类集(在设计时定义)编写代码,并优化这些类的代码和数据访问。在您的场景中,这是不可能的 - 使用非类型化数据集和动态创建的 SQL + ASP.NET 数据绑定控件,并希望它在大多数情况下都能工作。
编辑:
现在我看到您想要使用 MVC3 = 无数据绑定控件。您必须编写代码来检查非类型化数据集并生成一些 UI,以允许查看、编辑和验证具有您预先不知道的结构的数据。这通常意味着您需要自己的元模型来描述新创建的表、约束、字段大小、可为空列等,并使用此信息来创建 UI,也许还可以根据该信息准备非类型化数据集。
Application where tables are created dynamically by users is not scenario for Entity Framework. Entity framework requires compiled classes mapped to tables (+ mapping if not inferred by conventions) and your code must use these classes to take advantage of them.
In my opinion application where tables are created dynamically is not something where you can expect strongly typed access or great performance. Both these requirements expect that you write a code against well defined set of classes (defined at design time) and you optimize the code and data access for these classes. In your scenario this is not possible - use untyped datasets and dynamically created SQL + ASP.NET data bound controls and hope that it will work in most cases.
Edit:
Now I see that you want to use MVC3 = no data bound controls. You will have to write a code inspecting your untyped datasets and producing some UI to allow viewing, editing and validating the data with structure you don't know upfront. That generally means that you need your own meta model describing newly created tables, constrains, field sizes, nullable columns etc. and use this information to create UI and perhaps also prepare untyped datasets from that information.