在.net codefirst中我可以动态创建类吗?

发布于 2024-11-08 19:57:36 字数 416 浏览 4 评论 0原文

场景如下:

我正在编写一个数据加载器,它接受来自某些 DB2 目录表的二进制数据。我为 DB2 表结构创建了 POCO,并在将数据加载到 SQLServer DB 之前使用 Code First 创建 SQLServer DB。我首先使用代码并为每个数据集创建新数据库的原因之一是,我正在处理不同版本的 DB2,这些版本在版本之间添加了列、移动位置的列,甚至删除了列。

在开发过程中,我根据我的数据来自哪个版本的 DB2,对不同版本的 POCO 进行注释和取消注释。但现在我想要一个通用版本。一个明显的解决方案是拥有一组 POCO,并根据 DB2 版本对其名称进行后缀修复,以及一组 DBContext 类。这将需要使用 DBContext 的代码具有全新的复杂性。

那么有没有一种方法可以根据 DB2 版本动态生成 POCO,然后让 CodeFirst 从那里获取呢?

Here is the scenario:

I'm writing a data loader which accepts binary data from certain DB2 catalog tables. I created POCOs for the DB2 table structures and am using Code First to create the SQLServer DB just before loading the data into it. One of the reasons I'm using code first and creating the fresh DB for each data set, is that I'm dealing with different versions of DB2, which have added columns, columns that move locations and even dropped columns, between the versions.

During development, I comment and uncomment different versions of the POCOs, depending on which version of DB2 my data came from. But now I want to have a general purpose version. One obvious solution would be to have sets of these POCOs with a post fix on their name according to the DB2 version, as well as a set of DBContext classes. That will require a whole new level of complexity in the code that makes use of the DBContext.

So is there a way to generate the POCOs dynamically, according to the DB2 version, and then have CodeFirst take it from there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2024-11-15 19:57:36

如果我理解正确的话,您正在尝试从具有不同模式的不同 DB2 数据库获取数据,但希望将其加载到单个 SQL Server 模式中?

您可以考虑使用映射文件。下面的示例假设您有两个源 DB2 模式(“Schema1”和“Schema2”)和一个目标 SQL 模式。有一个类型(“Entity1”),但有两个 POCO 分别代表两个 DB2 模式(“Entity1FromSchema1”和“Entity1FromSchema2”)。

public class Entity1FromSchema1Mapping : EntityTypeConfiguration<Entity1FromSchema1>
{
    public Entity1FromSchema1Mapping()
    {
        this.ToTable("Entity1");
        this.HasKey( ... );
        this.Property( ... );
        ...            
    }
}

public class Entity1FromSchema2Mapping : EntityTypeConfiguration<Entity1FromSchema2>
{
    public Entity1FromSchema2Mapping()
    {
        this.ToTable("Entity1");
        this.HasKey( ... );
        this.Property( ... );
        ...            
    }
}

请注意,2 个不同实体的映射文件都映射到同一个 SQL Server 表。

If I understand correctly, you are trying to get data from different DB2 databases with somewhat different schemas but want to load it into a single SQL Server schema?

You could consider using a mapping file. The example below assumes you have two source DB2 schemas ("Schema1" and "Schema2") and one destination SQL schema. There is a single type ("Entity1") but two POCOs representing each of the two DB2 schemas ("Entity1FromSchema1" and "Entity1FromSchema2").

public class Entity1FromSchema1Mapping : EntityTypeConfiguration<Entity1FromSchema1>
{
    public Entity1FromSchema1Mapping()
    {
        this.ToTable("Entity1");
        this.HasKey( ... );
        this.Property( ... );
        ...            
    }
}

public class Entity1FromSchema2Mapping : EntityTypeConfiguration<Entity1FromSchema2>
{
    public Entity1FromSchema2Mapping()
    {
        this.ToTable("Entity1");
        this.HasKey( ... );
        this.Property( ... );
        ...            
    }
}

Note both mapping files for 2 different entities are mapped to the same SQL Server table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文