可以使用CompositeId的CustomSqlType? [英] Possible to use CustomSqlType with CompositeId?

查看:150
本文介绍了可以使用CompositeId的CustomSqlType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用遗留表,需要创建一个基于两个char(3)字段的CompositeId。没有看到任何重载,这使得这可能与流利。

我试图的映射如下所示:

  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));

我也试过:

<$ p (x => x.LegacyEntity1,LegacyEntity1Id)
.KeyReference(x => x.LegacyEntity2,LegacyEntity2Id) ;

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

这两个结果都是相同的结果 - 使用合适的合成id生成表,但两列都是默认的nvarchar(255)。因此,外键无法生成,我得到一个异常,因为父表是char(3)。



这不可能通过Fluent映射吗?

如果没有,映射它是否有任何真正的区别是这样的*:

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

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

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

*我可以稍微修改表格(足以添加标识),因为遗留表正被ETLed到一个本地的SQL实例。



或者还有另一种方法吗?在这种情况下,不能使用HasManyToMany,因为它的价值(将有一个有效载荷)。

解决方案

这是如何在2017年完成:

  CompositeId()
.KeyProperty(
x => x.LegacyEntity1Id,类型(AnsiString)。长度(3))
.KeyProperty(
x => x.LegacyEntity2Id,
k
k => k.ColumnName(LegacyEntity1Id类型(AnsiString)。Length(3))


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).

解决方案

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))

这篇关于可以使用CompositeId的CustomSqlType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆