Reputation: 65
I'm trying to use EF6 in my win form app. In my code when I'm adding a new object in the db I'm getting the null reference exception. Actually Products property in the method InitNewProducts is null. What am i doing wrong?
using System.Data.Entity;
namespace DAL
{
public class CartContext: DbContext
{
public CartContext(): base("DbConnection")
{
}
public DbSet<Product> Products;
}
}
using System.Collections.Generic;
namespace DAL
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set;}
}
}
private void InitNewProducts()
{
using (var context = new CartContext())
{
var product1 = new Product {Id = 1, Name = "SomeProduct1", CartItems = new List<CartItem> {new CartItem {Id = 1} } };
context.Products.Add(product1);
context.SaveChanges();
}
}
Upvotes: 0
Views: 59
Reputation: 4812
Add accessors to DbSet<Product>
:
public virtual DbSet<Product> Products {get; set;}
Upvotes: 1