I am studying Entity Framework Core. I have two simple classes: Country
and Currency
. Currencies should have a one-to-many relationship with Countries (France, Germany, Italy etc.. all share the Euro Currency).
I want to use the fluent API only, no data annotations. The goal is to use LINQ to get the Currency Name for a Country object stored in a MYSQL database.
I defined my classes:
public class Country
{
public string CountryID {get; set;} // ISO3166, "US" for United States
public string CurrencyID {get; set;} // ISO4217 "USD" for US Dollar
public string Name {get; set;}
public virtual Currency Currency {get; set;}
public Country() {};
}
public class Currency
{
public string CurrencyID {get; set;} // ISO4217
public string Name {get; set;}
public virtual ICollection<Country> Countries {get; set;}
public Currency(){};
}
I defined my DBSets objects in the DBContext
public DbSet<Country> Countries {get; set;}
public DbSet<Currency> Currencies {get; set;}
And I use the fluent API to define my data:
// Country
modelBuilder.Entity<Country>().HasKey(c => new { c.CountryID });
modelBuilder.Entity<Country>().Property(c => c.CountryID).IsRequired();
modelBuilder.Entity<Country>().Property(c => c.CountryID).HasMaxLength(2);
modelBuilder.Entity<Country>().Property(c => c.Name).IsRequired();
modelBuilder.Entity<Country>().Property(c => c.Name).HasMaxLength(50);
modelBuilder.Entity<Country>().Property(c => c.CurrencyID).IsRequired(); //Foreign Key
modelBuilder.Entity<Country>().Property(c => c.CurrencyID).HasMaxLength(3); //Foreign Key
// Currency
modelBuilder.Entity<Currency>().HasKey(c => new { c.CurrencyID });
modelBuilder.Entity<Currency>().Property(c => c.CurrencyID).IsRequired();
modelBuilder.Entity<Currency>().Property(c => c.CurrencyID).HasMaxLength(3);
modelBuilder.Entity<Currency>().Property(c => c.Name).IsRequired();
modelBuilder.Entity<Currency>().Property(c => c.Name).HasMaxLength(70);
Then I created some default data:
using (var dbContext = ContextFactory.Create(connectionString))
{
{
dbContext.Add(new Country(iso3166Code: "FR", name: "France", iso4217Code: "EUR"));
dbContext.Add(new Country(iso3166Code: "DE", name: "Germany", iso4217Code: "EUR"));
dbContext.Add(new Country(iso3166Code: "IT", name: "Italy", iso4217Code: "EUR"));
dbContext.Add(new Country(iso3166Code: "US", name: "United States of America", iso4217Code: "USD"));
dbContext.Add(new Currency(iso4217Code: "EUR", name: "Euro"));
dbContext.Add(new Currency(iso4217Code: "USD", name: "US Dollar"));
dbContext.SaveChanges();
}
I am sure the data are stored in MYSQL database and the server is running. I can connect to the database but when I try:
var italyCurrencyName = context.Countries.First(c => c.CountryID == "IT").Currency.Name;
WriteLine($"Italy currency is: {italyCurrencyName}");
I get an exception: Object reference not set to an instance of an object
. The reason is that the Currency navigation property of the Country object appears to be null.
The following code gets the right Country object, with "EUR" set as CurrencyID
(the foreign key) but the Currency
property is null
.
var italy = context.Countries.First(c => c.CountryID == "IT");
I tried to specify the relationship like this without success:
modelBuilder.Entity<Country>().HasOne(e => e.Currency).WithMany(e => e.Countries).HasForeignKey(e => e.CurrencyID);
modelBuilder.Entity<Currency>().HasMany(e => e.Countries);
What am I missing?
UPDATE:
I can get my data only if I force the loading of the Currencies DbSet:
var italy = context.Countries.First(c => c.CountryID == "IT");
var euro = context.Currencies.First(c => c.CurrencyID == "EUR");
WriteLine($"Italy currency is: {italyCurrencyName}");