I created a .net core console application project as follows. I think several months ago this can be run without error but now it does not work anymore with the following error.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
</ItemGroup>
</Project>
using Microsoft.EntityFrameworkCore;
using System;
namespace ConsoleApp4
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
class SchoolContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=MyDatabase.db");
}
public DbSet<Student> Students { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (SchoolContext _schoolContext = new SchoolContext())
{
Student student = new Student() { Name = "Billy" };
_schoolContext.Students.Add(student);
int result = _schoolContext.SaveChanges();
Console.WriteLine($"There is(are) {result} student(s) added.");
}
}
}
}
What causes this error and how to solve it?
For your reference, the database and table do exist as follows.
I am working on Visual Studio 2017 15.8.0 Preview 1.1.
After wasting several hours, I found the solution.
Unlike MyDatabase.db
generated by migration in the project root, MyDatabase.db
generated by default in $OutDir
does not have Students
table.
To solve it, I have to apply the following configuration on the root MyDatabase.db
.
Probably this issue is caused by VS 2017 Preview.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With