我有两个旧版 SQL 表:
Contact
Id(uniqueidentifier, not null)
Foo
GlobalId(nvarchar(50, null)
EntityName(nvarchar(100, null)
Foo.GlobalId 列存储数据库中其他表的 ID,因此要获取相关数据,我们将像这样连接表:
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);
}