Entity Framework Foreign Key attribute usage problems

ef-code-first ef-migrations entity-framework-6


I'm using Entity Framework 6 Code First to create an ASP.NET MVC 5 application, however I'm running into some issues with foreign keys. I present two tables, Country and Currency, to illustrate the issue.

The Country model depicts a table containing the characteristics of many nations:

public class Country {
    [Display(Name = "L_Country_ISO2", ResourceType = typeof(ResxGlobal))]
    public string iso { get; set; }

    [Display(Name = "L_Country", ResourceType = typeof(ResxGlobal))]
    public string name { get; set; }

    public string nicename { get; set; }

    [Display(Name = "L_Country_ISO3", ResourceType = typeof(ResxGlobal))]
    public string iso3 { get; set; }

    public short? numcode { get; set; }

    [Display(Name = "L_DialCode", ResourceType = typeof(ResxGlobal))]
    public int phonecode { get; set; }

The country's currency properties are listed in a separate table called Currency, whose PK is a two-letter ISO country code that also serves as an FK to the Country table:

public class Currency {
    [Display(Name = "L_Country_ISO2", ResourceType = typeof(ResxGlobal))]
    public string CountryCode { get; set; }

    [Required, StringLength(100)]
    [Display(Name="Currency name")]
    public string CurrencyName { get; set; }

    [Required, StringLength(3)]
    [Display(Name="Currency code")]
    public string CurrencyCode { get; set; }

    public string Symbol { get; set; }

    public virtual Country Country {get; set; }

So far, so good; let's now examine the second model (Currency). The SQL Server diagram shows a relationship between Country and Currencya with a PK (they small key) terminator at both ends of the relationship if I enable the ForeignKey attribute specifying that the CountryCode PK is also an FK to be used by the navigation property Country and complete the migration. Normally, the small key icon is shown on the PK table and the small infinity icon is shown at the FK end of the relationship. That struck me as odd.

I undid the migration, deleted the Foreign Key property (commented it out), and then updated the database once again. This time, the relationship appeared as I had anticipated, with PK holding a key and the other end an infinity symbol. The table, though, displayed an additional column that I did not include in my model. The extra column with the name "Country iso" that I had not provided in my model was manually removed from the migration.

When I try to use the database context to access the currency after removing the unneeded Country iso column (from the migration code) and updating the database, I see the "Invalid column name Country iso" issue. Why on earth does it keep insisting that I receive a column that I don't have? I don't see a reference to that anywhere, is it hidden somewhere in some metadata file?

So, how should an FK relationship (one to one and one to many) be represented in a model?

10/7/2014 5:06:23 PM

Popular Answer

10/7/2014 5:37:29 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow