无法解析 DbContext 构造函数 - 数据库创建错误
Cannot resolve DbContext constructor - database creation error
在我的代码中,EF7 没有创建实际正确的 sqlite 数据库。我假设问题的根源可能是以下错误,首先是代码片段:.
public class Database : DbContext
{
public Database()
: base("Database")
{
}
public virtual DbSet<Device> Devices { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "test2.sqlite" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
optionsBuilder.UseSqlite(connection);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
我正在使用 WPF 和 entity framework 7 与 Sqlite。我在尝试编译代码时收到的错误如下:
Cannot resolve constructor 'DbContext(string)', candidates are:
DbContext(Microsoft.Data.Entity.Infrastructure.DbContextOptions) (in
class DbContext) DbContext(System.IServiceProvider) (in class
DbContext)
我在MainWindow.xaml.cs
中调用数据库
public partial class MainWindow : Window
{
private Database _context = new Database();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_context.Devices.Add(new Device
{
ProductCode = "100",
TimeCreated = DateTime.Now
});
_context.SaveChanges();
System.Windows.Data.CollectionViewSource deviceViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("DeviceViewSource")));
//_context.Devices.Include(d => d.Name).Load();
//deviceViewSource.Source = _context.Devices.GetLocal();
//deviceViewSource.Source = _context.Categories.GetLocal();
}
我的代码编译时没有 :base("Database")
定义,但即使创建了 *.sqlite 文件,它也没有任何表并抛出此异常:
"SQLite Error 1: 'no such table: Device'"
Table 配置代码我在这个问题中遗漏了,如果需要可以添加它。普通控制台测试应用程序可以正常工作。
编辑:我正在使用代码优先方法创建数据库。
您好像错过了
的来电
_context.Database.EnsureCreated();
这将确保 1. 文件在那里并且 2. 表已创建。
:警告:不要在 DbContext 的构造函数中调用它。
在我的代码中,EF7 没有创建实际正确的 sqlite 数据库。我假设问题的根源可能是以下错误,首先是代码片段:.
public class Database : DbContext
{
public Database()
: base("Database")
{
}
public virtual DbSet<Device> Devices { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "test2.sqlite" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
optionsBuilder.UseSqlite(connection);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
我正在使用 WPF 和 entity framework 7 与 Sqlite。我在尝试编译代码时收到的错误如下:
Cannot resolve constructor 'DbContext(string)', candidates are: DbContext(Microsoft.Data.Entity.Infrastructure.DbContextOptions) (in class DbContext) DbContext(System.IServiceProvider) (in class DbContext)
我在MainWindow.xaml.cs
中调用数据库public partial class MainWindow : Window
{
private Database _context = new Database();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_context.Devices.Add(new Device
{
ProductCode = "100",
TimeCreated = DateTime.Now
});
_context.SaveChanges();
System.Windows.Data.CollectionViewSource deviceViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("DeviceViewSource")));
//_context.Devices.Include(d => d.Name).Load();
//deviceViewSource.Source = _context.Devices.GetLocal();
//deviceViewSource.Source = _context.Categories.GetLocal();
}
我的代码编译时没有 :base("Database")
定义,但即使创建了 *.sqlite 文件,它也没有任何表并抛出此异常:
"SQLite Error 1: 'no such table: Device'"
Table 配置代码我在这个问题中遗漏了,如果需要可以添加它。普通控制台测试应用程序可以正常工作。
编辑:我正在使用代码优先方法创建数据库。
您好像错过了
的来电_context.Database.EnsureCreated();
这将确保 1. 文件在那里并且 2. 表已创建。
:警告:不要在 DbContext 的构造函数中调用它。