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:
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:
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?