0

I have an EF opbject

public class Country
{
    [Key]
    public string CountryCode { get; set; }
    public string CountryName { get; set; }
}

as well as a generic item:

public class Item
{
    public int ItemID { get; set; }
    public virtual ICollection<Country> AvailableIn { get; set; }
}

the resulting Country table has the fields:

CountryCode
CountryName 
ItemItemID

How do I tell EF that I would like the relation to be build as an ItemAvailableCountry related table as opposed to modifying the Country table?

EDIT

Per a comment below I've included the relevant migration code if I add just the column in question

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<int>(
            name: "ItemItemID",
            table: "Country",
            isNullable: true);
        migrationBuilder.AddForeignKey(
            name: "FK_Country_Item_ItemItemID",
            table: "Country",
            column: "ItemItemID",
            principalTable: "Item",
            principalColumn: "ItemID");
    }

As it may be relevant - this is work in EF7

Alex C
  • 16,624
  • 18
  • 66
  • 98
  • Are you sure there is a `CountryName` column? it should be `Name` (as declared in your `Country` class). The `ItemItemID` is because it requires a foreign key to `Item` but you don't explicitly specify the name so it auto-choose that name (combined from the class name `Item` and its primary key `ItemID`). However I expected it to `Item_ItemID` rather than `ItemItemID`. – Hopeless Sep 27 '15 at 21:15
  • Apologies! That was actually a typo in copying on my part. I was trying to genericise the question and the called one field `Name` while the other was `CountryName` in the actual functioning applicaiton code the column and object name are both `CountryName`. I know what you mean about expecting to see Item_ItemID but it is EF7 - so perhaps they've removed the underscore. I'll update the main question with a copy of the relevant migration code. – Alex C Sep 27 '15 at 21:23
  • As per the duplicate: you have to add the join table as a class too your model. True many-to-many relations will be implemented later: https://github.com/aspnet/EntityFramework/issues/749. – Gert Arnold Sep 27 '15 at 22:05
  • @Gert Arnold - ahhh! you're a hero! EF7 Many to Many isn't implemented yet. I've been trying to wrap my head around the documentation of the changes to Fluent based on the only thing I could find : https://github.com/aspnet/EntityFramework/wiki/Entity-Framework-Design-Meeting-Notes-June-5,-2014 (some meeting notes from 2014) I'm happy to just implement the relationship "the hard way" if I know I'm not just missing something. – Alex C Sep 27 '15 at 22:27

1 Answers1

1

If you want a many to many relationship put an ICollection of Item on the country class and EF will generate the table you want.

Paul
  • 653
  • 6
  • 15
  • Thanks for the tip - the good news is that the odd column is no longer appearing on the Country table, but the relationship table doesn't seem to get created. Still digging. – Alex C Sep 27 '15 at 22:10