1

I try to explain better using an example:

  • I create the class Product with attibutes like that:

    private string _ProductCode = "";
    [DisplayName("ProductCode")]
    public String ProductCode
    {
       get { return _ProductCode; }
       set { _ProductCode = value; OnPropertyChanged("ProductCode"); }
    }
    
  • the class Product implements the interfaces: INotifyPropertyChanged, IEditableObject

  • I create the class ProductList that has the following property:

    public List<Product> Products { get; set; }
    
  • I bind the class Product in a DataGridView control using the DataSource property

    MyGridView.DataSource = ProductList.Products;
    
  • I have the table ProductListTable on db SqlServer that has more column than the class Product

  • When I push the button "Save" on my form I'd would like to save the ProductList on ProductListTable

I'm really confusing about the way to follow ... I have to use Link 2 Sql, Entity Framework? I'm searching for something simple for a small project, but using POCO classes for rapresenting the data layer.

Now how I should proceed for linking class to DB table?

1
  • I guess you are you using WPF, right?
    – margabit
    Aug 21, 2013 at 15:14

2 Answers 2

1

You can use Dapper. It is really simple, it boils down to having right sql. Here is example. PS: Use NuGet in VS and search for Dapper.

using(var con = new SqlConnection(connectionString))
{
    con.Open();
    con.Execute(
        @"insert ProductListTable(ProductCode, ...) values (@ProductCode, ...)",
        ProductList.Products
    );
}
0

It sounds like Entity Framework is what you want, specifically using the code first techniques.

With code first you're able to create a 'DbContext' class that can hold DbSet<> objects, similar to your List. These DbSets are linked to the database by entity framework and they will take care of the database interaction as long as you call SaveChanges() on them. This method will allow you to keep you POCO objects.

This book: Programming Entity Framework: Code First is great. It'll quickly bring you up to speed (only a few hundred pages).

I'll also add this as quite a full tutorial without buying a book: Code-First Development with Entity Framework 4

2
  • would you mind explaining more on what these resources do and why do you recommend these as answering the question asked? "Link-only answers" are not quite welcome at Stack Exchange
    – gnat
    Dec 13, 2013 at 12:00
  • Does that extra paragraph give enough information about EF Code First? Enough that he'd then want to use the links?
    – Ben Gale
    Dec 13, 2013 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy