DbContextBuilder不包含UseSqlite的定义。

7

我是 UWP 的新手,正在尝试设置我的项目来使用 SQLite 数据库,我正在使用 Microsoft.EntityFrameworkCore.SQLite。

当我尝试设置数据库上下文时,我不断地遇到这个错误。

DbContextOptionsBuilder 没有 UseSqlite 的定义,也没有可访问的扩展方法接受类型为 DBcontextoptionsbuilder 的第一个参数(您是否在使用指令或程序集引用)

下面是我的代码

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BradamaInternationalSkill.Models
{
    /// <summary>
    /// The Entity Framework Database Context.
    /// </summary>
    public class PersonContext : DbContext
    {
        internal DbSet<Person> People { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Filename=People.db");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Make Id required.
            modelBuilder.Entity<Person>()
                .Property(p => p.Id)
                .IsRequired();

            // Make Name required.
            modelBuilder.Entity<Person>()
                .Property(p => p.Name)
                .IsRequired();
        }
    }
}
1个回答

10

我通过卸载 "Microsoft Entity Framework Core" Nuget 包,安装了 "Entity.Framework.Core.Sqlite.Design" 解决了这个问题。我不知道为什么它能够解决问题,但是错误已经消失了,我很高兴,这才是最重要的。


这应该是正确的答案。你救了我的一天! - Thien Nhan Nguyen
1
"EntityFramework.Core.Sqlite" 也可以使用,我喜欢采用更通用的依赖项,以防未来更新中出现问题。 - axelrotter
我在尝试按照Microsoft的Raozer Pages官方教程来搭建一个类时遇到了同样的问题。我正在使用M2 Mac上的Visual Studio for Mac。在我的情况下,添加Microsoft.EntityFrameworkCore.Sqlite并没有解决这个问题。我不得不移除Microsoft.EntityFrameworkCore.SqlServer包,然后再次尝试搭建,这次它成功了,并且重新添加了sqlserver引用。不确定为什么会失败,但是在Visual Studio的.Net Framework上有一些东西无法正常工作(可能是因为它也无法通过命令行工作)。 - Enric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接