实体框架7和SQLite表未创建 [英] Entity Framework 7 and SQLite Tables not creating

查看:80
本文介绍了实体框架7和SQLite表未创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一段时间,以弄清楚如何使用单个DBContext以Code First的方式创建多个表而没有任何运气。我确定这只是我对框架不熟悉,但是我不确定我缺少什么。这是一个包含实体和DBContext的简单示例。

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);
    }
}

在我看来它应该可以工作,但是何时我在下面的代码中将其称为没有这样的表:MyEntity,在遇到第一个foreach循环时会抛出Sqlite异常。

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();
    }  

我几乎可以保证这很简单,但是我只是想念似乎找不到它,而且我在他们的文档中找不到任何示例。似乎在GitHub上有类似的问题,在这里 https://github.com/aspnet/EntityFramework/ issue / 2874 ,但这是针对多种情况的。所以也许这是另一个尚未发布的作品?

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 https://github.com/aspnet/EntityFramework/issues/2874 but that's for multiple contexts. So maybe this is another piece that just hasn't quite made it to release yet?

解决方案

按照在 http:// ef上发布的教程进行操作@natemcmaster建议的.readthedocs.org / en / latest / getting-started / uwp.html 和@ lukas-kabrt建议的解决方案,我能够使它按预期工作。通过运行以下命令,我能够创建表并从中插入/选择数据。

By following the tutorial posted on http://ef.readthedocs.org/en/latest/getting-started/uwp.html as suggested by @natemcmaster and the solution recommended by @lukas-kabrt I was able to get it to work as desired. By running the below commands I was able to get the tables created and insert/select data from them.

Install-Package EntityFramework.Commands –Pre
Add-Migration MyFirstMigration
Update-Database


推荐答案

DbContext 类包含数据库的配置-表,关系等。要使用 DbContext 您需要创建一个与您的 DbContext 匹配的数据库。在代码优先的世界中,这是通过数据库迁移完成的。

The DbContext class contains configuration of your database - tables, relationsships, etc. To use the DbContext you need to create a database that matches your DbContext. In the Code-first world, it is done by database migrations.

对于 ASP.NET 5 ,您需要将此配置添加到 project.json 文件

For ASP.NET 5 you need to add this configuration to your project.json file

"commands": {
    "ef": "EntityFramework.Commands"
}

然后向您的项目添加迁移并通过运行以下命令将此迁移应用于数据库

and then add a migration to your project and apply this migration to the DB by running following commands

dnx ef migrations add MyFirstMigration
dnx ef database update 

对于完整.NET ,您需要在 Package中运行以下命令管理器控制台(工具‣NuGet软件包管理器‣软件包管理器控制台)

For Full .NET you need to run following commands in the Package Manager Console (Tools ‣ NuGet Package Manager ‣ Package Manager Console)

Install-Package EntityFramework.Commands –Pre
Add-Migration MyFirstMigration
Update-Database

这篇关于实体框架7和SQLite表未创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆