レガシー SQL テーブルが 2 つあります。
Contact
Id(uniqueidentifier, not null)
Foo
GlobalId(nvarchar(50, null)
EntityName(nvarchar(100, null)
Foo.GlobalId 列には、他のテーブルの ID が DB に格納されるため、関連するデータを取得するには、次のようにテーブルを結合します。
select * from Foo
inner join Contact on Foo.EnitityName = 'contact'
and cast(Foo.Globalid as unqiqueidentifier) = Contact.Id
FluentAPI を使用してこの関係を表現したり、nvarchar を uniqueidentifier (またはその逆) にキャストしたり、失敗した場合、LINQ を使用して SQL cast() を実行するにはどうすればよいですか?
public class Contact
{
public Guid Id {get;set;}
public virtual ICollection<Foo> Foos {get;set;}
}
public class ContactMap : EntityTypeConfiguration<Contact>
{
HasKey(t => t.Id);
HasMany(c => c.Foos)
.WithOptional(foo => foo.Contact)
/* ??? */;
}
public class Foo
{
public string GlobalId {get;set;}
public Contact Contact {get;set}
}
public class FooMap : EntityTypeConfiguration<Foo>
{
Property(t => t.PsmsGlobalId).HasMaxLength(50);
Property(t => t.PsmsEntityName).HasMaxLength(100);
}