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?
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.
精彩评论