1

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

  }
ocuenca
  • 38,548
  • 11
  • 89
  • 102
Mark
  • 2,175
  • 20
  • 23

2 Answers2

0

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.

scokmen
  • 571
  • 3
  • 19
  • have added Currency to CrossRate class. However VS complainining about the s.Currency in ".HasRequired(s => s.Currency)" – Mark May 15 '15 at 10:57
  • Seems better.However, VS now complains "EntityType 'CrossRate' has no key defined. Define the key for this EntityType" – Mark May 15 '15 at 11:47
  • [Key] attribute is required for primary keys. See the topic here : http://stackoverflow.com/questions/13862013/entitytype-category-has-no-key-defined-define-the-key-for-this-entitytype – scokmen May 15 '15 at 11:55
  • Hmm on ToCurrencyId property? – Mark May 15 '15 at 11:58
  • Yes both ToCurrencyId and FromCurrencyId properties. I updated the answer. – scokmen May 15 '15 at 12:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77895/discussion-between-mark-h-and-scokmen). – Mark May 15 '15 at 12:08
  • Unfortunately this was not the answer. – Mark May 15 '15 at 13:32
0

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 });
Community
  • 1
  • 1
ocuenca
  • 38,548
  • 11
  • 89
  • 102