0

I am trying to build a pretty extensive database heavy web application and doing so with little experience. What I am trying to figure out is creating a relational table in entity framework through scaffolding.

It seems I have accidentally done this already with these two models, but I have no idea how it happened:

namespace FlavorPing.Models
{
    public class MenuItem
    {

        [Key]
        public int MenuItemID { get; set; }

        public string ItemName { get; set; }

        public string Description { get; set; }

        //Category
        //May need to put this back and add to controllers and views.
        //[ForeignKey("Merchant")]
        //public int MerchantID { get; set; }

        public virtual Merchant Merchant { get; set; }

        public ICollection<Follower> Followers { get; set; }

    }
}

public class Merchant
{
    //Meant to inherit identity.
    [ForeignKey("ApplicationUserId")]
    public string ApplicationUserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }


    [Key]
    public int MerchantID { get; set; }

    [Required]
    [Display(Name = "Business Name")]
    public string MerchantName { get; set; }

    [Required]
    [Display(Name = "Email")]
    [DataType(DataType.EmailAddress)]
    public string email { get; set; }

    //need to create formatting here.
    [Required]
    [Display(Name = "Web Site Link")]
    public string website { get; set; }

    public int MenuItemID { get; set; }

    public virtual List<MenuItem> MenuItems { get; set; }

    public virtual MerchantDetails MerchantDetails { get; set; }
}

The above models have their own dedicated tables, but a second table MenuItemFollowers was created with the MenuItemID and FollowerID as columuns, which I want, but I have no idea how I did this and need to know so I could add another ID to this table.

David Tansey
  • 5,813
  • 4
  • 35
  • 51
JP Hochbaum
  • 637
  • 4
  • 15
  • 28
  • Take a look at the accepted answer in this post: http://stackoverflow.com/questions/19342908/how-to-create-a-many-to-many-mapping-in-entity-framework -- I think that may answer your question. – David Tansey Dec 09 '15 at 18:47
  • 1
    EF created MenuItemFollowers by convention since you have a many to many relationship. If you want more fields, you can model that table and add additional attributes. http://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table – Steve Greene Dec 09 '15 at 18:48
  • Thanks for the help, gonna have to read these over. – JP Hochbaum Dec 09 '15 at 21:04
  • It appears I have to create my own association table, correct? And just avoid scaffolding it? – JP Hochbaum Dec 09 '15 at 21:36

0 Answers0