Code First from exisiting database how to use relationships

2

I have a database with 3 tables. Tours, Offers and TourOffers. Tours contains the locations and prices of tours. Offers are the special offer on tours and TourOffers stores what tours have what offers.

Examples below:

enter image description here

When I produced the code first from existing DB two classes were created from the table Tours and Offers.

How do I use the ToursOffers tables to keep to the relationship between the tables? For example The tours go in a combo box how can I display the selected tours offer in a textbox.

My functionaning code:

private void onload(object sender, EventArgs e)//Populate Product Name drop down. 
    {
        Tours tour = new tours();

        dataSource = tour.GetAll();
        comboBox1.DisplayMember = "Tour"; // set display member
        comboBox1.ValueMember = "PID"; // value member as Id to use at selectedIndex changed
        comboBox1.DataSource = dataSource;

        offerSource = tour.GetAllOffers();

    }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            comboBox1.DataSource = dataSource;
            decimal price = dataSource.Where(x => x.PID == (int)comboBox1.SelectedValue)
                            .FirstOrDefault().Price.Value;

            textBox1.Text = price.ToString("0.00"); // set result to two decimal places
            textBox2.Text = //RELATED OFFER
}

An idea I had gives:

 string offers = offerSource
                 .Where(x => x.Equals(comboBox1.SelectedValue))
                 .FirstOrDefault().OfferDescription; 

System.NullReferenceException occurred HResult=0x80004003
Message=Object reference not set to an instance of an object.

EDIT 1

Tours generated class:

    public partial class Tour
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public Tour()
            {
                Offers = new HashSet<Offer>();
            }

            public int PID { get; set; }

            [Required]
            [StringLength(40)]
            public string Tour { get; set; }

            [Column(TypeName = "money")]
            public decimal? Price { get; set; }

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<Offer> Offers { get; set; }
        }

Offers generated class:

public partial class Offer
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public Offer()
            {
                Products = new HashSet<Product>();
            }

            public int OfferID { get; set; }

            [Required]
            [StringLength(60)]
            public string Desc { get; set; }

            [StringLength(10)]
            public string Code { get; set; }
    }

And Model:

public Model1()
    : base("name=Model1")
{
}

public virtual DbSet<Offer> Offers { get; set; }
public virtual DbSet<Tour> Tours { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Offer>()
        .Property(e => e.Code)
        .IsFixedLength();

    modelBuilder.Entity<Offer>()
        .HasMany(e => e.Tours)
        .WithMany(e => e.Offers)
        .Map(m => m.ToTable("TourOffers").MapLeftKey("OfferID").MapRightKey("PID"));

    modelBuilder.Entity<Tour>()
        .Property(e => e.Price)
        .HasPrecision(19, 4);
}
c#
winforms
entity-framework
ef-code-first
asked on Stack Overflow Sep 29, 2017 by Phil3992 • edited Sep 29, 2017 by Phil3992

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0