2012-08-06 57 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

相关问题