2013-02-08 24 views
1

我有兩個傳統的SQL表:如何指定需要SQL轉換的EF關係?

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

回答

0

實體框架目前不支持SQL類型轉換。創建一個執行演員的SQL視圖對我來說效果很好。