如何动态地将 DataSet 对象转换为 ObjectContext(实体框架)对象?
我有一个现有的 SQL Server 数据库,其中存储来自大型特定日志文件(通常为 100 MB 或更多)的数据,每个数据库一个。经过一番分析,数据库再次被删除。
我通过 Visual Studio 设计器从数据库创建了实体框架模型和数据集模型。 DataSet仅用于通过SqlBulkCopy批量导入数据,经过相当复杂的解析过程。然后使用实体框架模型完成所有查询,其 CreateQuery
方法通过这样的接口公开
public IQueryable<TTarget> GetResults<TTarget>() where TTarget : EntityObject, new()
{
return this.Context.CreateQuery<TTarget>(typeof(TTarget).Name);
}
现在,有时我的文件非常小,在这种情况下我想省略导入到数据库中,但只有数据的内存表示形式,可以作为实体访问。我们的想法是创建 DataSet,但不是批量导入,而是直接将其传输到可通过接口访问的 ObjectContext 中。
这有意义吗?
现在,这是到目前为止我为此转换所做的工作:我遍历 DataSet 中的所有表,将单行转换为相应类型的实体,并将它们添加到我的类型化实体上下文类的实例化对象中,就像这样
MyEntities context = new MyEntities(); //create new in-memory context
///....
//get the item in the navigations table
MyDataSet.NavigationResultRow dataRow = ds.NavigationResult.First(); //here, a foreach would be necessary in a true-world scenario
NavigationResult entity = new NavigationResult
{
Direction = dataRow.Direction,
///...
NavigationResultID = dataRow.NavigationResultID
}; //convert to entities
context.AddToNavigationResult(entity); //add to entities
///....
一项非常繁琐的工作,因为我需要为每个实体类型创建一个转换器,并迭代我拥有的数据集中的每个表。请注意,如果我更改了数据库模型...
另外,我还发现,如果我向 SQL Server 数据库提供有效的连接字符串,则只能实例化 MyEntities
。由于我不想每次都实际写入成熟的数据库,这阻碍了我的意图。我打算只拥有一些内存代理数据库。
我可以做得更简单吗? 是否有一些自动化的方法来执行此类转换,例如从 DataSet 对象生成 ObjectContext?
PS:我看到了一些关于单元测试的问题,这些问题似乎有些相关,但不太准确。
I have an existing SQL Server database, where I store data from large specific log files (often 100 MB and more), one per database. After some analysis, the database is deleted again.
From the database, I have created both a Entity Framework Model and a DataSet Model via the Visual Studio designers. The DataSet is only for bulk importing data with SqlBulkCopy
, after a quite complicated parsing process. All queries are then done using the Entity Framework Model, whose CreateQuery
Method is exposed via an interface like this
public IQueryable<TTarget> GetResults<TTarget>() where TTarget : EntityObject, new()
{
return this.Context.CreateQuery<TTarget>(typeof(TTarget).Name);
}
Now, sometimes my files are very small and in such a case I would like to omit the import into the database, but just have a an in-memory representation of the data, accessible as Entities. The idea is to create the DataSet, but instead of bulk importing, to directly transfer it into an ObjectContext which is accessible via the interface.
Does this make sense?
Now here's what I have done for this conversion so far: I traverse all tables in the DataSet, convert the single rows into entities of the corresponding type and add them to instantiated object of my typed Entity context class, like so
MyEntities context = new MyEntities(); //create new in-memory context
///....
//get the item in the navigations table
MyDataSet.NavigationResultRow dataRow = ds.NavigationResult.First(); //here, a foreach would be necessary in a true-world scenario
NavigationResult entity = new NavigationResult
{
Direction = dataRow.Direction,
///...
NavigationResultID = dataRow.NavigationResultID
}; //convert to entities
context.AddToNavigationResult(entity); //add to entities
///....
A very tedious work, as I would need to create a converter for each of my entity type and iterate over each table in the DataSet I have. Beware, if I ever change my database model....
Also, I have found out, that I can only instantiate MyEntities
, if I provide a valid connection string to a SQL Server database. Since I do not want to actually write to my fully fledged database each time, this hinders my intentions. I intend to have only some in-memory proxy database.
Can I do simpler? Is there some automated way of doing such a conversion, like generating an ObjectContext out of a DataSet object?
P.S: I have seen a few questions about unit testing that seem somewhat related, but not quite exact.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些在对象之间进行映射的工具,例如 automapper。这是一个非常好的开源工具。
然而,这些工具有时会出现问题,例如生成重复的实体键,或者当被映射的对象的结构非常不同时出现问题。
如果您尝试自动化它,我认为如果您使用 EF 4 和 POCO 对象,它工作的机会更大。
如果您最终手动编写映射代码,我会将其移至一个单独的过程中,并对其进行自动单元测试。
我们这样做的方法是创建一个带有“映射”方法的静态类:
然后为每个方法编写一个测试,检查字段是否正确映射。
There are tools that map between objects, such as automapper. This is a very good open source tool.
However, these tools sometimes have problems, for example generating duplicate entity keys, or problems when the structure of the objects being mapped are very different.
If you are trying to automate it, I think that there is a greater chance of it working if you use EF 4 and POCO objects.
If you end up writing the mapping code manually, I would move it into a seperate procedure with automated unit tests on it.
The way we do this is to create a static class with "Map" methods":
Then write a test for each method in which we check that the fields were mapped correctly.