Frage

In EF5, using a code first approach and fluent API, how can create a navigation property (many to 1) based on a key that is not the primary key of the other table?

My database model is as such, and I can't change it :

  • TableFoo1
    • Foo1ID int (key)
    • Field1 int
    • Value varchar
  • TableFoo2
    • Foo2ID int (key)
    • Field1 int
    • Value varchar
  • TableBar
    • BarID int (key)
    • Field1 int
    • Value varchar

I want my these entities :

public class Foo1
{
    public int Foo1ID { get; set; }
    public int BarID { get; set; }    //It corresponds Bar.ID
    public string Value { get; set; }
    public Bar Bar { get; set; }
}

public class Foo2
{
    public int Foo2ID { get; set; }
    public int BarID { get; set; }    //It corresponds to Bar.Field1, NOT Bar.ID
    public string Value { get; set; }
    public Bar Bar { get; set; }
}

public class Bar
{
    public int ID { get; set; }
    public int Field1 { get; set; }
    public string Value { get; set; }
}

Here are my maps (the 2nd one is the incomplete one) :

public  class Foo1Map : EntityTypeConfiguration<Foo1>
{
    public Foo1Map()
    {
        this.HasKey(e => e.Foo1ID);

        // Links to Bar's Primary Key, yay, it's easy.
        this.HasRequired(e => e.Bar).WithMany().HasForeignKey(e => e.BarID);
    }
}

public  class Foo2Map : EntityTypeConfiguration<Foo2>
{
    public Foo2Map()
    {
        this.HasKey(e => e.Foo2ID);

        // No clues of how to links to Bar's other unique column, but not primary key.
        this.HasRequired(e => e.Bar).WithMany()........?
    }
}

public class BarMap : EntityTypeConfiguration<Bar>
{
    public BarMap()
    {
        this.HasKey(e => e.BarID);
    }
}
War es hilfreich?

Lösung

The case I asked in the questions, where Foo1 and Foo2 both link to Bar with different keys (on the Bar side) can't be done in EF5 (more details in the post this question is an almost duplicate of) as is. However, if all your objects (Foo1 and Foo2 here) need to map on the same column (with a unicity constraint), even if it's not the PK of the other object (Bar here) in the mapping you can set that column to be the key. Also, if you do not want to use the same column for Foo1 and Foo2, but you will never need Foo1 and Foo2 in the same instance of the context, you could use dynamic mapping, but it's dangerous and has a lot of complications.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top