2012-08-06 53 views
4

我正在構建一個連接到舊數據庫(無法修改)的EF4的新服務。實體框架4與非鍵列/字段和不同列/字段名稱的關聯

(這BD似乎已經由DB天才,你會看到的骯髒的工作,我知道建的,但我必須這樣做)

基本上,我有兩個表(SegurosPassageirosLocsPassageiros),其鍵與常規方式與其他表相關聯。如以下模型所示,它們與PK/FK沒有物理關聯,但它們通過非鍵列「SegurosPassageiros.CodPassageiro」和「LocsPassageiros.CodLocPassageiroInterno」鏈接。 此外,該協會是一個一對多:

LocsPassageiros 1 ... * SegurosPassageiros

我發現與非重點的關聯很多答案,但不能同時非鍵和不同的名稱在同一場景中。

的表:

LocsPassageiros 
---------------------------------- 
CodLocPassageiro (PK) | int 
Nome     | varchar 
CodLocPassageiroInterno | int 
---------------------------------- 


---------------------------------- 
SegurosPassageiros 
---------------------------------- 
CodSeguroPassageiro(PK) | int 
CodPassageiro   | int 
---------------------------------- 

代碼(類 「SeguroPassageiro」 映射到表 「SegurosPassageiros」):

public class SeguroPassageiro 
{ 
    [Key] 
    public Int32 CodSeguroPassageiro { get; set; } 

    .... (other fields) 

    //tried this, no success 
    //[ForeignKey("Passageiro")] 
    public virtual Int32 CodLocPassageiroInterno { get; set; } 

    //tried this annotation with no success 
    [Association("Passageiro_Seguros", "CodPassageiro", "CodLocPassageiroInterno")] 
    public virtual Passageiro Passageiro { get; set; } 
} 

類 「Passageiro」 映射到表 「LocsPassageiros」:

public class Passageiro 
{ 
    [Key] 
    public Int32 CodLocPassageiro { get; set; } 

    ... (other fields) 

    //tried this, no success 
    //[ForeignKey("Seguros")] 
    public Int32 CodLocPassageiroInterno { get; set; } 

    //tried these annotations with no success 
    [ForeignKey("CodLocPassageiroInterno")] 
    [Association("Passageiro_Seguros", "CodLocPassageiroInterno", "CodPassageiro")] 
    public List<SeguroPassageiro> Seguros { get; set; } 
} 

設置型號:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

     modelBuilder.Entity<Dominio.Aereo.Passageiro>().ToTable("LocsPassageiros"); 
     modelBuilder.Entity<Dominio.Aereo.SeguroPassageiro>() 
     .ToTable("SegurosPassageiros"); 

     //Tried both lines below (together and separeted) with no success:    
     //modelBuilder.Entity<Dominio.Aereo.SeguroPassageiro>().HasRequired(s => s.Passageiro).WithOptional(); 
     //modelBuilder.Entity<Dominio.Aereo.Passageiro>().HasMany(p => p.Seguros).WithRequired(); 

     //Tried "linking" the column "CodPassageiro" on table "SegurosPassageiros" to     
     //column "CodLocPassageiroInterno" on table "LocsPassageiros". No success. 
     modelBuilder.Entity<Dominio.Aereo.SeguroPassageiro>().Property(s => s.CodLocPassageiroInterno).HasColumnName("CodPassageiro"); 
    } 

在這種模式下,幾十個試驗,我能到達漸漸在Passageiro對象,但與錯誤相關聯的列表中最接近後: 「SegurosPassageiros.CodPassageiro」和「LocsPassageiros.CodLocPassageiro」(而不是「LocsPassageiros .CodLocPassageiro * Interno *「)。 EF堅持要得到錯誤的關聯(在LocsPassageiros上獲得PK)。

任何人都知道我怎麼能在EF中獲得這個關聯?

回答

2

這是一個工作。 (如果我有權將其作爲註釋發佈,我不會將其作爲回答發佈)。 可以使用EF查詢並將兩個實體連接到Where中,而不是直接使用該關聯。

+0

你是對的。我已經實現了這種方式。像魅力一樣工作,唯一的缺點是現在我有很多選擇(根據記錄數量大約3或4)。 乾杯隊友! – 2012-08-07 21:39:41

+0

很高興爲你工作..我有特權發表評論..感謝您接受此爲答案.. :) – sak 2012-08-08 04:58:27

3

不幸的是,這是不可能的。 EF可以僅在主體實體中定義的PK之上建立一對多關係。在你的情況下,只有當LocsPassageiros.CodLocPassageiro被用作主鍵時。

在數據庫中,只有主鍵唯一時纔可以構建這樣的關係 - 它必須是主鍵或者它必須具有唯一的約束。如果數據庫不符合這些要求,則該關係是無效的。 EF目前不支持唯一約束,因此唯一的方法是使用主鍵。

+0

是啊!這就是我害怕的。 我將不得不實施加入(將有很多選擇,但沒有其他方式) 乾杯反正隊友! – 2012-08-07 21:37:09