代码之家  ›  专栏  ›  技术社区  ›  Hary

从List<T>而不是从连接字符串创建DataContext

  •  0
  • Hary  · 技术社区  · 6 年前

    有没有办法创造 System.Data.Linq.DataContext List<T>

    MSDN 必须传递带有任何连接字符串的构造函数来启动对象。

    列表<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); 
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Wasif Mahmood Mustafa    6 年前

    您可以这样做,但请记住,它将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