Possible to use CustomSqlType with CompositeId?

2019-07-26 07:00发布

问题:

Working with legacy tables, need to create a CompositeId based on two char(3) fields. Don't see any overloads that make this possible with Fluent.

The mapping I'm attempting looks like this:

CompositeId()
.KeyProperty(x => x.LegacyEntity1Id, "LegacyEntity1Id")
.KeyProperty(x => x.LegacyEntity2Id, "LegacyEntity2Id");

Map(x => x.LegacyEntity1Id).CustomSqlType("char(3)");
Map(x => x.LegacyEntity2Id).CustomSqlType("char(3)");

I've also tried:

CompositeId()
    .KeyReference(x => x.LegacyEntity1, "LegacyEntity1Id")
    .KeyReference(x => x.LegacyEntity2, "LegacyEntity2Id");

Map(x => x.LegacyEntity1Id).CustomSqlType("char(3)");
Map(x => x.LegacyEntity2Id).CustomSqlType("char(3)");

Both result in the same outcome - the table gets generated with a proper composite id, but both columns are the default nvarchar(255). As a result, the foreign keys fail to generate and I get an exception, since the parent tables are char(3).

Is this not possible to map via Fluent?

If not, is there any real difference in mapping it like this*:

Id(x => x.Id).GeneratedBy.Identity();

Map(x => x.LegacityEntity1Id).CustomSqlType("char(3)");
Map(x => x.LegacityEntity2Id).CustomSqlType("char(3)");

References(x => x.LegacityEntity1).Column("LegacityEntity1Id").UniqueKey("1").Not.Nullable();
References(x => x.LegacityEntity2).Column("LegacityEntity2Id").UniqueKey("1").Not.Nullable();

* I do have the ability to modify the tables slightly (enough to add an identity), since the legacy tables are being ETLed into a local SQL instance.

Or is there another alternative approach? Can't use a HasManyToMany in this case, for what it's worth (will have a payload).

回答1:

KeyReference will search the map of the referenced entity and uses the sqltype there. go to the referenced entities and specify Id(x => x.Id).Length(3).



回答2:

This is how to do it in 2017:

CompositeId()
  .KeyProperty(
     x => x.LegacyEntity1Id, 
     k => k.ColumnName("LegacyEntity1Id").Type("AnsiString").Length(3))
  .KeyProperty(
     x => x.LegacyEntity2Id, 
     k => k.ColumnName("LegacyEntity2Id").Type("AnsiString").Length(3))