0

I've created a database with SSMS. Then I did the whole C# project and installed EF with NuGet. I wanted EF to create all the classes and a context for me, so I did Code First from Database and it did that for me. Now the Product class for example looks like this:

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

        public int ID { get; set; }

        [Required]
        [StringLength(50)]
        public string Name { get; set; }

        public int Type_ID { get; set; }

        public decimal Price { get; set; }

        public string Descryption { get; set; }

        public int Available_amount { get; set; }

        public virtual Product_Type Product_Type { get; set; }

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

The thing is, Product table looks like this:

enter image description here

It is related to Orders and Product_Type tables. Now when I want to get all the products and move them to dataGridView - this happens:

The code which gets the products:

public void GetProducts()
{
    using (var db = new SklepContext())
    {
        var data = db.Products.ToList();
        dataGridViewBrowse.DataSource = data;
    }
}

The effect:

enter image description here

Firstly it was throwing errors at my face, so I had to add this line this.Configuration.ProxyCreationEnabled = false; in my SklepContext constructor.

The question is what can I do to make it not read those virtual members (don't know why EF would add them in the first place) or make EF make those classes without them (I cannot delete them just by deleting those 2 lines in Product class), so my dataGridView shows only values from the database?

minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57
  • There are a few options, which are exaplined here https://www.ryadel.com/en/enable-or-disable-lazyloading-in-entity-framework/ – pmcilreavy Dec 20 '17 at 00:48
  • I don't quite see one in those that would solve the issue though :( – minecraftplayer1234 Dec 20 '17 at 00:56
  • "it was throwing errors at my face". What do you mean by that? Why did you set `ProxyCreationEnabled` into `false`? See [What are the downsides to turning off ProxyCreationEnabled for CTP5 of EF code first](https://stackoverflow.com/a/4596787/6138713) – jegtugado Dec 20 '17 at 01:12
  • Just errors, had to click `Ok` with adding every row. I just don't want those two last columns when binding my data to `dataGridView` – minecraftplayer1234 Dec 20 '17 at 01:14

1 Answers1

1

Your title is very misleading if you suddenly say

I just don't want those two last columns when binding my data to dataGridView

The reason why those two columns appear in your dataGridView is because you are binding your models directly.

Here are some alternatives:

1.Remove the columns after binding.

dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);
dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);

2.Create a different view model for binding

public class DataGridViewModel
{   
    public int ID { get; set; }

    public string Name { get; set; }

    public int Type_ID { get; set; }

    public decimal Price { get; set; }

    public string Descryption { get; set; }

    public int Available_amount { get; set; }

    public DataGridViewModel()  
    {    
    }
}

public void GetProducts()
{
    using (var db = new SklepContext())
    {
        var data = db.Products.Select(r => new DataGridViewModel()
        {
            ID  = r.ID,
            Name = r.Name,
            Type_ID = r.Type_ID,
            Price = r.Price,
            Descryption = r.Descryption,
            Available_amount = r.Available_amount
        }).ToList();
        dataGridViewBrowse.DataSource = data;
    }
}
jegtugado
  • 5,081
  • 1
  • 12
  • 35