1

I've implemented a content management system that deals with a certain kind of object, and now the client has introduced another kind of object, whose structure and behavior is exactly the same, but separate. They want to be able to switch modes, where each mode behaves identically, except that one deals with one object type and the other with the other.

Suppose the first object type is stored in a table called TBL_OBJECT, and the second in one called TBL_OBJECT_NEW. I'm looking for a way to use my existing code, but to be able to switch on the fly and have it switch to the other table. Because the structure is identical, this is conceptually possible. The only problem is that in L2E, the table name is saved as attribute and compiled in, so I'm looking for a way to hook the sql statement creation process, so that my existing statements like this:

Dim db as New DataContext
Dim query = From obj in db.Objects

will resolve to the TBL_OBJECT_NEW table instead of the TBL_OBJECT table. If I can do this, I can reuse a huge amount of code. I know it's a somewhat unusual scenario, but I'm really hoping it's possible. Any help much appreciated! Thanks.

By the way, I'm using LinqConnect from DevArt for this; I don't know how much a plain L2E approach will apply to this. On the other hand, if you know how to do it with DevArt but NOT L2E, that would still be very helpful.

4

1 回答 1

1

我们不知道将两个相同的表映射到一个概念实体的任何可能性。

LinqConnect和实体框架中都有一个解决方案。例如,您可以尝试在只有一个差异的两个模型之间切换(第一个模型将映射 TBL_OBJECT,第二个模型 - TBK_OBJECT_NEW)。

LinqConnect 解决方案

要在 LinqConnect 中具有相同结构的不同表之间切换,您可以创建两个 .lqml 文件并从其中一个文件加载当前 DataContext 实例的映射:

string mappingFileName;
if (...) // Check whether TBL_OBJECT_A should be used
  mappingFileName = "MyDataContext_A.lqml";
else 
  mappingFileName = "MyDataContext_B.lqml";
Stream contextStream = System.IO.File.OpenRead(mappingFileName);
System.Data.Linq.Mapping.MappingSource mappingSource =
Devart.Data.Linq.Mapping.XmlMappingSource.FromStream(contextStream);
MyDataContext context = new MyDataContext(connectionString, mappingSource);

此外,我们计划支持流畅的映射(在代码中设置映射而不使用属性或映射文件的方法),但目前无法提供任何时间框架。您可以在我们的UserVoice投票支持此功能。

实体框架解决方案

将元数据工件处理属性设置为 CopyToOutputDirectory。在此之后转到生成的 SSDL 文件并将表名从“TBL_OBJECT”更改为“TBL_OBJECT_NEW”。现在唯一要做的就是使用正确的连接字符串创建上下文(您可以将新的连接字符串添加到应用程序配置文件或将整个连接字符串传递给ObjectContext构造函数)。

于 2011-02-14T17:12:17.910 回答