none
Need Help with Entity Framework Core and Sqlite RRS feed

  • Question

  • I’m trying to learn how to use EF Core and Database First to generate a context class and entity classes for a Sqlite database in the latest release of VB 2019.

    Here is database ValuePairs.db.

    CREATE TABLE "IntegerValuePairs" (

                  "KeyName"        TEXT,

                  "ValueName"     TEXT,

                  "Value" INTEGER,

                  PRIMARY KEY("KeyName","ValueName")

    )

    CREATE TABLE "RealValuePairs" (

                  "KeyName"        TEXT,

                  "ValueName"     TEXT,

                  "Value" REAL,

                  PRIMARY KEY("KeyName","ValueName")

    )

    CREATE TABLE "TextValuePairs" (

                  "KeyName"        TEXT,

                  "ValueName"     TEXT,

                  "Value" TEXT,

                  PRIMARY KEY("KeyName","ValueName")

    )

    I create a project for a Console App (.NET Core) for C#, then add a folder App_Data with my database in it.

    I then run the following three commands in Package Manager Console.

    • Install-Package Microsoft.EntityFrameworkCore.Tools
    • Install-Package Microsoft.EntityFrameworkCore.Sqlite
    • Scaffold-DbContext "DataSource=C:\VSS_NET\ValuePairs\App_Data\ValuePairs" Microsoft.EntityFrameworkCore.Sqlite  -OutputDir Models

    A file with the context class, ValuePairsContext.cs, is created.  Here it is

    using System;

    using Microsoft.EntityFrameworkCore;

    using Microsoft.EntityFrameworkCore.Metadata;

    namespace ValuePairs.Models

    {

        public partial class ValuePairsContext : DbContext

        {

            public ValuePairsContext()

            {

            }

            public ValuePairsContext(DbContextOptions<ValuePairsContext> options)

                : base(options)

            {

            }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

            {

                if (!optionsBuilder.IsConfigured)

                {

    #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.

                    optionsBuilder.UseSqlite("DataSource=C:\\VSS_NET\\ValuePairs\\App_Data\\ValuePairs");

                }

            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)

            {

                OnModelCreatingPartial(modelBuilder);

            }

            partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

        }

    }

    But there are no entity classes for the three tables.  What am I doing wrong?

    Thanks.


    Sunday, December 29, 2019 8:55 AM

All replies

  • Hi GoatWrangler,
    Based on your description, I can’t reproduce your problem.
    About "how to use EF Core and Database First to generate a context class and entity classes for a Sqlite database",
    you can refer to these documents.
    [Getting Started with EF Core]
    [SQLite]
    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    Hope these are helpful for you.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 30, 2019 6:16 AM