mercredi 4 novembre 2015

Entity Framework 7 and SQLite Tables not creating

I've been trying for awhile to figure out how to use a single DBContext to create multiple tables in a Code First fashion without any luck. I'm sure it's just my unfamiliarity with the framework but I'm not sure what I'm missing. Here's a simple example with entities and the DBContext.

[Table("MyEntity")]
public class MyEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string MyColumn { get; set; }
    public int MyNumber { get; set; }
}

[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 MyContext : DbContext
{
    public DbSet<MyEntity> MyTable { get; set; }
    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);
    }
}

It looks to me like it should work, but when I call it in the below code it blows up with a 'no such table: MyEntity' Sqlite exception when hitting the first foreach loop.

static void Main(string[] args)
    {
        using (var db = new MyContext())
        {

            MyEntity testEntity1 = new MyEntity();
            MySecondEntity entity1 = new MySecondEntity();

            testEntity1.MyColumn = "Test Data 1";
            testEntity1.MyNumber = 12345;

            db.MyTable.Add(testEntity1);
            db.Database.Migrate();

            entity1.MyColumn = "New Data 1";
            entity1.MyNumber = 2;

            db.MyTable2.Add(entity1);
            db.Database.Migrate();

            Console.WriteLine("Inserting Data...");

            Console.WriteLine("Data in the Database");

            foreach (var entity in db.MyTable)
            {
                Console.WriteLine("Id: " + entity.Id);
                Console.WriteLine("Column Data: " + entity.MyColumn);
                Console.WriteLine("Number: " + entity.MyNumber);
            }

            foreach (var entity in db.MyTable2)
            {
                Console.WriteLine("Id: " + entity.Id);
                Console.WriteLine("Column Data: " + entity.MyColumn);
                Console.WriteLine("Number: " + entity.MyNumber);
            }
        }

        Console.WriteLine("Examples run finished,press Enter to continue...");
        Console.ReadLine();
    }  

I can almost guarantee it's something simple I'm missing but I just can't seem to find it, and there aren't any examples I can find in their documentation. There seems to be a similar issue submitted on GitHub here http://ift.tt/20um6da but that's for multiple contexts. So maybe this is another piece that just hasn't quite made it to release yet?

Aucun commentaire:

Enregistrer un commentaire