So I'm working on an application that is using Entity Framework 7 and SQLite to store various pieces of data. I'm running into an issue with EF not creating the second table in my test.db file. I have a hunch that it's because both context's are setting the datasource to test.db
var connectionStringBuilder = new SqliteConnectionStringBuilder {DataSource = "test.db"};
I'm not sure if this would cause an issue or not as I'm not familiar with EF or SQLite and this is the first ORM that I've worked with. Below I have two example enity's with their respective context's.
[Table("MyEntity")]
public class MyEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string MyColumn { get; set; }
public int MyNumber { get; set; }
}
public class MyContext : DbContext
{
public DbSet<MyEntity> MyTable { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder {DataSource = "test.db"};
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
optionsBuilder.UseSqlite(connection);
}
}
[Table("MySecondEntity")]
public class MySecondEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string MyColumn { get; set; }
public int MyNumber { get; set; }
}
public class MySecondContext : DbContext
{
public DbSet<MySecondEntity> MyTable2 { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "test.db" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
optionsBuilder.UseSqlite(connection);
}
}
These two entities and their contexts are in separate C# files. And both are being referenced in Program.cs. Below you'll find the code that's in my Program.cs file.
static void Main(string[] args)
{
using (var db = new MyContext())
{
db.Database.EnsureCreated();
PopulateData(db);
CheckValues(db);
}
using (var db = new MySecondContext())
{
db.Database.EnsureCreated();
PopulateDataTwo(db);
CheckValuesTwo(db);
}
WaitForCompletion();
}
private static void PopulateData(MyContext db)
{
MyEntity testEntity1 = new MyEntity();
testEntity1.MyColumn = "Test Data 1";
testEntity1.MyNumber = 12345;
db.Add(testEntity1);
db.SaveChanges();
}
private static void PopulateDataTwo(MySecondContext db)
{
MySecondEntity entity1 = new MySecondEntity();
entity1.MyColumn = "New Data 1";
entity1.MyNumber = 2;
db.Add(entity1);
db.SaveChanges();
}
private static void CheckValues(MyContext db)
{
Console.WriteLine("Inserting Data...");
Console.WriteLine("Data in the Database");
foreach (var entity in db.MyTable)
{
WriteToConsole("Id: " + entity.Id);
WriteToConsole("Column Data: " + entity.MyColumn);
WriteToConsole("Number: " + entity.MyNumber);
}
}
private static void CheckValuesTwo(MySecondContext db)
{
Console.WriteLine("Inserting Data...");
Console.WriteLine("Data in the Database");
foreach (var entity in db.MyTable2)
{
WriteToConsole("Id: " + entity.Id);
WriteToConsole("Column Data: " + entity.MyColumn);
WriteToConsole("Number: " + entity.MyNumber);
}
}
public static void WriteToConsole(string msg)
{
Console.WriteLine(msg);
}
private static void WaitForCompletion()
{
WriteToConsole("Examples run finished,press Enter to continue...");
Console.ReadLine();
}
I have a feeling that it's just something simple I'm missing, when I run this code I get the below error. The error happens in the db.SaveChanges();
call in PopulateDataTwo()
.
Message=An error occurred while updating the entries. See the inner exception for details. Message=SQLite Error 1: 'no such table: MySecondEntity'
I don't see anything different between the two entity classes other than the name that would indicate an issue with why it wouldn't be able to create the second table. Does each context need to point to a unique datasource?
Aucun commentaire:
Enregistrer un commentaire