none
Walkthrough not working - not translating POCO name to database table correctly

    Question

  • I tried the EF 4.2 Code First walkthrough and it's not working for me.

    http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx

    It's asking for a non-existent collection.

    In my console I have:

        class Program
        {
            static void Main(string[] args)
            {
                using (var db = new BlogContext())
                {
                    foreach (var item in db.Categories)
                    {
                        Console.WriteLine(item.CategoryName);
                    }
                }
    
                Console.WriteLine("Press any key to Exit");
                Console.ReadLine();
            }
        }

    My context looks like this:

        public class BlogContext : DbContext
        {
            public IDbSet<Blog> Blogs { get; set; }
            public IDbSet<BlogCategory> Categories { get; set; }
            public IDbSet<BlogPost> Posts { get; set; }
            public IDbSet<BlogComment> Comments { get; set; }
        }

    My BlogCategory class looks like this:

        public class BlogCategory
        {
            #region Properties
            public int Id { get; set; }
            public string CategoryName { get; set; }
            public string rowversion { get; set; }
            public virtual ICollection<Blog> BlogList { get; set; }
            #endregion
        }

    And my app.config looks like this:

    <configuration>
        <configSections>
            <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </configSections>
        <entityFramework>
            <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
                <parameters>
                    <!--<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />-->
                    <parameter value="Data Source=KWLatitude-PC\sqlserver2008;Initial Catalog=BlogApp; Integrated Security=True; MultipleActiveResultSets=True" />
                </parameters>
            </defaultConnectionFactory>
        </entityFramework>
    </configuration>

    When I step-through and look at the generated SQL it's creating, the following:

    db.Categories = {SELECT
    [Extent1].[Id] AS [Id],
    [Extent1].[CategoryName] AS [CategoryName],
    [Extent1].[rowversion] AS [rowversion]
    FROM [dbo].[BlogCategories] AS [Extent1]}


    You can see that this doesn't exist, since the database table is [dbo].[BlogCategory] (singular), not BlogCategories.  Why this is happening I don't know, since the example walk-through has a similar pattern where the model is singular and the datacontext property is plural.

    Any ideas?

    Thanks.



    • Edited by kwilder Friday, May 04, 2012 10:45 PM
    Friday, May 04, 2012 10:44 PM

Answers

All replies

  • I see what happened, it created a new database instead of using the one in the app.config. It seems I need to include a connectionString section.  I'm wondering what all that other EF configuration is if I can't use it.

    When I add a connectionString section and update the BlogContext with the connectionStringName, it now throws an exception saying the, BlogCategories table doesn't exist.

    Can anyone see what I'm doing wrong?

    I want to be able to use POCO's with database-first.

    Thanks.

    Here's my new Context:

        public class BlogContext : DbContext
        {
            public BlogContext()
                : base("name=BlogContext")
            {
    
            }
            public IDbSet<Blog> Blogs { get; set; }
            public IDbSet<BlogCategory> Categories { get; set; }
            public IDbSet<BlogPost> Posts { get; set; }
            public IDbSet<BlogComment> Comments { get; set; }
        }

    Here's my new app.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </configSections>
        <entityFramework>
            <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
                <parameters>
                    <!--<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />-->
                    <parameter value="Data Source=KWLatitude-PC\sqlserver2008;Initial Catalog=BlogApp; Integrated Security=True; MultipleActiveResultSets=True" />
                </parameters>
            </defaultConnectionFactory>
        </entityFramework>
        <connectionStrings>
            <add name="BlogContext" connectionString="Data Source=KWLatitude-PC\sqlserver2008;Initial Catalog=BlogApp; Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
        </connectionStrings>
    </configuration>

    Friday, May 04, 2012 10:54 PM
  • Hi kwilder,

    Welcome to MSDN Forum.

    The walkthrough you read is about code-first, it will generate the database by the entities you have defined in the code. If you want to use POCO with an existing database, please refer to the tutorial below.

    POCO in the Entity Framework: Part 1 - The Experience

    POCO in the Entity Framework : Part 2 – Complex Types, Deferred Loading and Explicit Loading

    POCO in the Entity Framework : Part 3 – Change Tracking with POCO

    This tutorial also contains source code, you can download it for learning. If you have any problems during the learning, please feel free to post in the forum, we will do our best to help you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 07, 2012 3:10 AM
  • Hi kwilder,

    Welcome to MSDN Forum.

    The walkthrough you read is about code-first, it will generate the database by the entities you have defined in the code. If you want to use POCO with an existing database, please refer to the tutorial below.

    POCO in the Entity Framework: Part 1 - The Experience

    POCO in the Entity Framework : Part 2 – Complex Types, Deferred Loading and Explicit Loading

    POCO in the Entity Framework : Part 3 – Change Tracking with POCO

    This tutorial also contains source code, you can download it for learning. If you have any problems during the learning, please feel free to post in the forum, we will do our best to help you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Allen,

    I'll take a look at these articles.  I was actually looking for an alternative like DbContext that doesn't need to use the EDMX designer. 

    The Walkthrough article I mentioned talked about what to do if you have a database, it said you just need to include the connection string in the context, this doesn't work as I expected.

    Is there a way to us the Entity Framework, some version 4.x or greater, either with ObjectContext or DbContext that doesn't involve the EDMX designer and I can use it with database-first?  I have a specific naming convention for my database tables where they are all singular and I'd like to keep that convention.

    Any help is appreciated.

    Thanks.

    Monday, May 07, 2012 5:20 AM
  • Hi kwilder,

    Yes, you can use the existing database without EDMX file, please refer to this article by Scott Gu - Using EF “Code First” with an Existing Database.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 07, 2012 5:36 AM
  • Hi kwilder,

    Yes, you can use the existing database without EDMX file, please refer to this article by Scott Gu - Using EF “Code First” with an Existing Database.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Allen,

    This article helped by showing the "OnModelCreating" override method.  I see it has changed a little since this article was written but it still helps a lot.  Unfortunately it doesn't seem like there's very much detailed documentation on DbContext anywhere.  I did find the documentation on DbContext, but it's not very complete.

    This series helped me a lot so far.  It also helped with concurrency issues.

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc

    Thanks.

    Monday, May 07, 2012 5:19 PM
  • Hi kwilder,

    Glad to see you again. I have answered the question in your another thread, please refer here.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 09, 2012 2:10 AM