Search code examples
asp.net-mvc-3entity-frameworkdatabase-designvirtual

Difficulty using "public virtual" variables in database creation


I am creating two tables in Visual Web Developer 2010 Express using the following code:

https://i.sstatic.net/qd7hu.jpg (sorry, the forum will not let me, due to a new account, post pictures directly)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BarApp.Models
{
    public class Drinks
    {
        public int DrinksId { get; set; }
        public int EstablishmentsID { get; set; }
        public string name { get; set; }
        public decimal price { get; set; }
        public string description { get; set; }
        public string image { get; set; }

        public virtual Establishments establishment { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BarApp.Models
{
    public class Promotions
    {
        public int PromotionsId { get; set; }
        public string name { get; set; }
        public float discount { get; set; }
        public int EstablishmentId { get; set; }
        public int DrinkId { get; set; }
        public string description { get; set; }

        public virtual Establishments establishment { get; set; }
        public virtual Drinks drink { get; set; }
    }
}

But when I look at the created tables, the Promotions table has actual rows for the public virtual code, whereas the Drinks table does not.

I am able to have the Drinks table function the way I want elsewhere in the project, but I cannot get Promotions to behave the same way because it appears that "public virtual" is giving different results in each table.

I do not understand why my Promotions table is actually creating rows for the public virtual variables. Can someone help me understand?


Solution

  • In this particular situation I was not using the conventional way of referencing the Id field of another table.

    I simply needed to change the following:

    public int EstablishmentId { get; set; } public int DrinkId { get; set; }

    to the following:

    public int EstablishmentsId { get; set; } public int DrinksId { get; set; }

    My tables were created as expected after I made this change.