locked
Do you think this tutorial is wrong or outdated RRS feed

  • Question

  • User887623398 posted

    Hello,

    I'm learning the Entity framework core and encountered in this tutorial:

    You can see the page here:

    https://entityframeworkcore.com/querying-data-basic-query

    And in there comes:

    Let's say we have a simple model which contains three entities.

    public class Customer
    {
        public int CustomerId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public virtual List<Invoice> Invoices { get; set; }
    }

    Load All data

    The following example loads all the data from Customers table.

    using (var context = new MyContext())
    {
        var customers = context.Customers.ToList();
    }

    This code isn't working. Is this wrong or outdated?

    thanks,

    Friday, January 15, 2021 8:54 PM

Answers

  • User-821857111 posted

    I mean this part:

    using (var context = new MyContext())
    {
        var customers = context.Customers.ToList();
    }
    
    

    Is correct? Visual studio doesn't accept it and appears red line under it!

    If you have red squiggles, I'm pretty certain that you have simply copied and pasted the code as-is and expected it to work. Have you actually created a DbContext called "MyContext" that has a DbSet called "Customers"? If not, the code will never work for you.

    That site is not a tutorial site. It's just a collection of snippets of code. You should go through a step-by-step tutorial for beginners like mgebhard suggested. You also need to review the basics of C#, I suggest.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 16, 2021 9:13 AM
  • User753101303 posted

    Hi,

    It's close from code that works. For now it seems they don't show or shown earlier how to declare thre MyContext class My guess is that this class is not defined correctly (or at all?l). As pointed earlier it would be easier to guide you knowing which error message you have. We don't even know if this is a compile time or runtime error.

    Edit: see for example https://entityframeworkcore.com/approach-code-first if you need guidance about the MyContext class though it seems they switched at some point from Order/OrderDetail to Customer which perhaps doesn't help you to get something up and running.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 17, 2021 6:49 PM

All replies

  • User475983607 posted

    This code isn't working. Is this wrong or outdated?

    The answer depends on what you mean by "isn't working".   

    Friday, January 15, 2021 9:11 PM
  • User887623398 posted

    I mean this part:

    using (var context = new MyContext())
    {
        var customers = context.Customers.ToList();
    }

    Is correct? Visual studio doesn't accept it and appears red line under it!

    Friday, January 15, 2021 9:55 PM
  • User475983607 posted

    I mean this part:

    using (var context = new MyContext())
    {
        var customers = context.Customers.ToList();
    }
    
    

    Is correct? Visual studio doesn't accept it and appears red line under it!

    There is nothing technically wrong with the code.  The example assumes a fundamental understanding of EF Core.   

    I recommend the official documentation for learning EF Core.

    https://docs.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli

    Saturday, January 16, 2021 2:05 AM
  • User-821857111 posted

    I mean this part:

    using (var context = new MyContext())
    {
        var customers = context.Customers.ToList();
    }
    
    

    Is correct? Visual studio doesn't accept it and appears red line under it!

    If you have red squiggles, I'm pretty certain that you have simply copied and pasted the code as-is and expected it to work. Have you actually created a DbContext called "MyContext" that has a DbSet called "Customers"? If not, the code will never work for you.

    That site is not a tutorial site. It's just a collection of snippets of code. You should go through a step-by-step tutorial for beginners like mgebhard suggested. You also need to review the basics of C#, I suggest.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 16, 2021 9:13 AM
  • User887623398 posted

    But the site claims that was a tutorial nat snippet. Anyway, there is another site for the entity-framework core.

    I think that's from you, It is a cool site. I learned many things from that.

    Sunday, January 17, 2021 5:15 PM
  • User-821857111 posted

    I have got one at learnentityframeworkcore.com. It has got a couple of step-by-step walkthroughs, but they probably need updating.

    https://www.learnentityframeworkcore.com/walkthroughs

    Sunday, January 17, 2021 5:52 PM
  • User753101303 posted

    Hi,

    It's close from code that works. For now it seems they don't show or shown earlier how to declare thre MyContext class My guess is that this class is not defined correctly (or at all?l). As pointed earlier it would be easier to guide you knowing which error message you have. We don't even know if this is a compile time or runtime error.

    Edit: see for example https://entityframeworkcore.com/approach-code-first if you need guidance about the MyContext class though it seems they switched at some point from Order/OrderDetail to Customer which perhaps doesn't help you to get something up and running.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 17, 2021 6:49 PM