none
Links to tutorials RRS feed

  • Question

  • I'm new to using Entity Framework, and found a tutorial that shows me how to save an entrance with Entity Framework.

    Now I want to learn how to get a datatable, update the database table and also delete an entrance in the database.

    Can anyone give me a link to that?

    Right now, I have this (getting an instance):

     public DataTable AnsiCrm_GetArea(int areaId)
            {
                try
                {
                    using (var context = new CrmContext())
                    {
                        var area = context.AnsiCrm_Area.Find(areaId);

                        //What have I to do here to convert it to a DataTAble?

                        return null;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

    What shall I write to convert the instance to a DataTable?

    Best regards,

    simsen :-)

    Tuesday, September 22, 2015 10:22 PM

Answers

  • What shall I write to convert the instance to a DataTable?

    You shouldn't be using a datatable with an ORM (EF). You defeat the purpose of using an ORM. A datatable is just a container based on outdated yesteryear technology.

    var area = context.AnsiCrm_Area.Find(areaId);

    What you have there is an object that was retuned from a query, which is area. 

    Area contains all the data that was returned from the database table based on the virtual object defined for the database table that's on the ORM (EF) virtual model/database

    var name = area.Name;

    var address = area.Address;

    That's if the database table AnsiCrm that AnsiCrm virtual model object was derived from had a Name and Address column.

    So what should be returned is this:

    public AnsiCrm AnsiCrm_GetArea(int areaId)

    An EF entity/object is a data container.  

    If the function was to return a collection of AnsiCrm objects because you did a Linq query that returned a collection of AnsiCrm objects, then you would return a List<T> of AnsiCrm, because that is what EF has created for you off of the virtual model is collection of virtual objects in a List<T> .

    public List<AnsiCrm> AnsiCrm_GetAreas()

    The reason you are using an ORM,  using a List<T>  and generics over a datatable is in the link, and a datatable uses boxing and unboxing that affects performance. A List<T> is a container too that holds a collection of objects with objects being containers in themselves.  

    http://lauteikkehn.blogspot.com/2012/03/datatable-vs-list.html

    https://en.wikipedia.org/wiki/Object-relational_mapping

    Please, toss datasets and datatables out of the picture and learn how to use objects, because that is what an ORM is about. 

    Maybe, you should start with a basic tutorial. The VB tutorial has the pictures that the C# one has lost.

    http://www.vbforums.com/showthread.php?540421-Tutorial-An-Introduction-to-the-ADO-NET-Entity-Framework

    • Marked as answer by simsen1965 Wednesday, September 23, 2015 3:57 PM
    Tuesday, September 22, 2015 11:17 PM

All replies

  • What shall I write to convert the instance to a DataTable?

    You shouldn't be using a datatable with an ORM (EF). You defeat the purpose of using an ORM. A datatable is just a container based on outdated yesteryear technology.

    var area = context.AnsiCrm_Area.Find(areaId);

    What you have there is an object that was retuned from a query, which is area. 

    Area contains all the data that was returned from the database table based on the virtual object defined for the database table that's on the ORM (EF) virtual model/database

    var name = area.Name;

    var address = area.Address;

    That's if the database table AnsiCrm that AnsiCrm virtual model object was derived from had a Name and Address column.

    So what should be returned is this:

    public AnsiCrm AnsiCrm_GetArea(int areaId)

    An EF entity/object is a data container.  

    If the function was to return a collection of AnsiCrm objects because you did a Linq query that returned a collection of AnsiCrm objects, then you would return a List<T> of AnsiCrm, because that is what EF has created for you off of the virtual model is collection of virtual objects in a List<T> .

    public List<AnsiCrm> AnsiCrm_GetAreas()

    The reason you are using an ORM,  using a List<T>  and generics over a datatable is in the link, and a datatable uses boxing and unboxing that affects performance. A List<T> is a container too that holds a collection of objects with objects being containers in themselves.  

    http://lauteikkehn.blogspot.com/2012/03/datatable-vs-list.html

    https://en.wikipedia.org/wiki/Object-relational_mapping

    Please, toss datasets and datatables out of the picture and learn how to use objects, because that is what an ORM is about. 

    Maybe, you should start with a basic tutorial. The VB tutorial has the pictures that the C# one has lost.

    http://www.vbforums.com/showthread.php?540421-Tutorial-An-Introduction-to-the-ADO-NET-Entity-Framework

    • Marked as answer by simsen1965 Wednesday, September 23, 2015 3:57 PM
    Tuesday, September 22, 2015 11:17 PM
  • Thank you very much.

    I haven't been around anything the past 3 years, so my knowledge is old.

    With your answer, I have enough to google

    best regards,

    simsen :-)

    Wednesday, September 23, 2015 3:58 PM