0

I have two models that looks like this:

   [Table("Titles")]
    public partial class Title
    {
        [Key]
        [Column("TitleId")]


        public virtual string Genre { get; set; }

        [ForeignKey("Genre")] // I want to link using the Genre field
        public virtual Genre GenreInfo { get; set; }

    }

    [Table("Genres")]
    public partial class Genre
   {

        [Key]       
        [Column("GenreID")]
        public int GenreID { get; set; } *** This is the actual PK in the table


        public string Genre { get; set; } // This contains unique genre code.

        public string Keywords { get; set; }

     }

The foreignKey in the title table is the field "Genre" not the GenreId. How do I define the relationship in the Title model that loads the genre infor using the Genre field?

It is a many to one relationship. (Titles can have only one genre)

Arcadian
  • 4,312
  • 12
  • 64
  • 107
  • Surely you mean Many to One. Or do you mean each Genre has one Title. – Aron Jan 10 '14 at 03:22
  • 'Genre': member names cannot be the same as their enclosing type ...? – pravprab Jan 10 '14 at 03:33
  • You can only have foreign keys to properties that are a primary key in the EF model (not necessarily in the database). You better define the association on GenreID (if possible). Otherwise you can only make ad hoc joins. – Gert Arnold Jan 10 '14 at 20:16

2 Answers2

1

Can you try this one ... ?

public partial class Title
{
    public virtual string Genre { get; set; }
    public virtual Genre GenreInfo { get; set; }
}


public partial class Genre
{
    public int GenreID { get; set; }
    public string sGenre { get; set; } // This contains unique genre code.
    public string Keywords { get; set; }

}

and in DbContext

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       modelBuilder.Entity<Title>()
       .HasOptional(b => b.GenreInfo)
       .WithRequired().Map(x => x.MapKey("sGenre")); 
    }
pravprab
  • 2,301
  • 3
  • 26
  • 43
0

I wouldn't model the classes that way. I'd do this:

    public class Title
    {
        public int Id   //or TitleId if you prefer

        public int GenreId { get; set; }

        public virtual Genre Genre { get; set; }
    }


   public class Genre
   {
        public int Id { get; set; }   //or GenreId if you prefer

        ["Required"]
        public string Name{ get; set; }

        public string Keywords { get; set; }
   }
  1. No data annotations or fluent API required to define the relationship because everything follows convention

  2. You can change the Name of the Genre without having to update the foreign keys in the Title table

  3. Joins tables on a foreign key that is an int rather than a string - so likely to be more efficient

Now you have to make sure that Name is unique. I do that my adding an index to the natural key and validating in the DbContext with ValidateEntity. More about that here

Note that you will soon be able to specify an index using data annotations

Community
  • 1
  • 1
Colin
  • 22,328
  • 17
  • 103
  • 197