如何在dbml文件中创建动态表? [英] How to create dynamic table in dbml file?

查看:137
本文介绍了如何在dbml文件中创建动态表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用linq to sql.我在dbml文件中有一个表,该表的某些字段可以动态生成(在运行时).有什么方法可以动态地向表中添加字段,还是动态地向dbml文件中添加类?

I'm using linq to sql. I have a table in dbml file that some of its fields can be generate dynamically (at run time). Is there any way to add fields to a table dynamiccaly , or add a class to dbml file dynamically?

推荐答案

您不能将类动态添加到DBML文件,但是可以创建

You can't add a class to the DBML file dynamically, but you can create a TypeBuilder instance to create a class at runtime with the appropriate TableAttribute and ColumnAttribute attributes and then use that class in your queries.

这样做的问题是,您必须通过反射来访问它,包括大多数生成查询的调用(因为您无法在编译时知道如何调用那些成员).

The problem with this is that you have to access this through reflection, including most of the calls to generate the queries (since you have no way of knowing at compile time what to call those members).

也就是说,最好的选择是使用更通用的容器(取决于您的数据形状),然后将其投影到一系列

That said, your best bet would be to use a more generic container (depending on your data shape) and then project that into a sequence of KeyValuePair<TKey, TValue> instances (or some other structure) which would hold the value keyed on the name of the dynamic field.

这篇关于如何在dbml文件中创建动态表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆