c# - 从列表创建 DataContext而不是来自连接字符串
问题描述
有没有办法System.Data.Linq.DataContext
从List<T>
从MSDN中,必须传递带有任何连接字符串的构造函数来启动对象。
我只是想知道有没有办法打破这个并List<T>
通过扩展DataContext
或其他东西来创建上下文?
期待类似这样的事情,
Class TestEntity
{
public int EntityId { get; set; }
public string EntityName { get; set; }
}
var list = new List<TestEntity>();
list.Add(new TestEntity { EntityId = 1, EntityName = "aaa" });
list.Add(new TestEntity { EntityId = 2, EntityName = "bbb" });
//Creating context on-demand with any available objects rather than external resources (database, file etc.)
var context = new DataContext(list);
解决方案
您可以执行类似的操作,但请记住,它将 item.EntityName 作为服务器。
var list = new List<TestEntity>();
list.Add(new TestEntity { EntityId = 1, EntityName = "aaa" });
list.Add(new TestEntity { EntityId = 2, EntityName = "bbb" });
//Creating context on-demand with any available objects rather than external resources (database, file etc.)
foreach (var item in list)
{
var context = new DataContext(item.EntityName);
context.CreateDatabase();
}
输出
Server=aaa;Database=DataContext;Integrated Security=SSPI
Server=bbb;Database=DataContext;Integrated Security=SSPI
推荐阅读
- wordpress - Woocommce:分类产品页面,产品按自定义分类法分组
- gitlab - 如何在 GitLab 评论中添加待办事项以增加待办事项数量?
- c++ - 如何强制 std::vector 使用移动构造函数而不是复制构造函数?
- ios - 为什么 Chrome 在 iOS 上请求下载两次?
- java - java - 如何使用spark和java处理并行中多个文件的不同位置?
- c++ - 选择排序不抓取数组中的最后一个元素
- ios - 查找最低定位元素
- javascript - 无法在聊天应用程序中使用 Ajax 调用从控制器返回 html
- python - 将多个数据帧转换为多索引数据帧
- c# - 选择不同的项目时,组合框属性更改属性不更新?