简体   繁体   中英

Entity Framework - Relationships

I am looking to create Currency and CrossRates in Entity Framework.

From a SQL perspective the CrossRates table would be quite simple.

Date      |FromCurrency|ToCurrency|Rate
01/01/2000|USD         |EUR       |1.5
01/01/2000|EUR         |USD       |0.67

How do I take the above idea and apply it within Entity Framework?

Here is what I have so far...

  public class Currency
  {
     public int Id { get; set; }
     public string Name { get; set; }

    //navigation
     public virtual List<CrossRate> CrossRates { get; set; } 
   }

  public class CrossRate
  {
      public int FromCurrencyId {get;set;}
      public int ToCurrencyId {get;set;}
      public DateTime Date {get;set;}
      public decimal Rate {get;set;}

  }

You can override context's OnModelCreating() method to define relationships. See the tutorial http://www.entityframeworktutorial.net/

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        //one-to-many 
        modelBuilder.Entity<CrossRate>()
                    .HasRequired<Currency>(s => s.FromCurrency)
                    .WithMany(s => s.CrossRates)
                    .HasForeignKey(s => s.FromCurrencyId );

        modelBuilder.Entity<CrossRate>()
                    .HasRequired<Currency>(s => s.ToCurrency)
                    .WithMany(s => s.CrossRates)
                    .HasForeignKey(s => s.ToCurrencyId );
}

You also should add

    public Currency ToCurrecy{get;set;}
    public Currency FromCurrecy{get;set;}

to your CrossRate class. For relationship, ICollection is better approach than List.

I think you need to create two one-to-many relationships, so Currency must have two collection of CrossRate . You can't have single collection referenced by two FKs, unless the PK of the another entity is composite (like in this post ), but Currency only have one PK. Try with this model:

public class Currency
{
    public int Id { get; set; }
    public string Name { get; set; }

    //navigation prop, these are the CrossRates where this Currency was used as a From
    public virtual ICollection<CrossRate> FromCrossRates { get; set; }

    //navigation prop, these are the CrossRates where this Currency was used as a To
    public virtual ICollection<CrossRate> ToCrossRates { get; set; }
}

public class CrossRate
{
    public int FromCurrencyId { get; set; }
    public int ToCurrencyId { get; set; }

    public DateTime Date { get; set; }
    public decimal Rate { get; set; }

    public Currency FromCurrency { get; set; }

    public Currency ToCurrency { get; set; }
}

And the relationships configuration would be:

        //composite PKs of CroassRate entity
        modelBuilder.Entity<CrossRate>().HasKey(cr => new {cr.FromCurrencyId,   cr.ToCurrencyId});

        //one-to-many 
        modelBuilder.Entity<CrossRate>()
                    .HasRequired(s => s.FromCurrency)
                    .WithMany(s => s.FromCrossRates)
                    .HasForeignKey(s => new { s.FromCurrencyId });

        modelBuilder.Entity<CrossRate>()
                   .HasRequired(s => s.ToCurrency)
                   .WithMany(s => s.ToCrossRates)
                   .HasForeignKey(s => new { s.ToCurrencyId });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM