简体   繁体   English

Linq-to-Sqlite是否支持使用Code First方法创建动态表?

[英]Does Linq-to-Sqlite support dynamic table creation using Code First approach?

I am trying to run a demo app to do a POC on Linq-to-Sqlite. 我正在尝试运行一个演示应用程序以在Linq-to-Sqlite上进行POC。 I'm using the latest System.Data.Sqlite NuGet package and .Net Framework 4.5 to create sample console application which will only create the database file initially. 我正在使用最新的System.Data.Sqlite NuGet程序包和.Net Framework 4.5创建示例控制台应用程序,该示例控制台应用程序只会在最初创建数据库文件。 The code simply creates an object of Movie class and adds it to the DbSet property on the DbContext class. 该代码仅创建了Movie类的对象,并将其添加到DbContext类的DbSet属性中。 However, on SaveChanges(), i get a System.Data.Entity.Infrastructure.DbUpdateException with the message "no such table: Movie". 但是,在SaveChanges()上,我收到一个System.Data.Entity.Infrastructure.DbUpdateException,并显示消息“没有这样的表:电影”。 Below is my code: 下面是我的代码:

if (!File.Exists(_databaseFilePath))
    SQLiteConnection.CreateFile(_databaseFilePath);

using (var dbContext = new MovieAppContext())
{
    dbContext.Movies.Add(new Movie(1, "Titanic", 7.5));
    var retVal = dbContext.SaveChanges();
}

public class MovieAppContext : DbContext
{
    public MovieAppContext():base("MovieAppConStr")
    {

    }

    public MovieAppContext(string connectionString)
        : base(connectionString)
    { }

    public DbSet<Movie> Movies { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Chinook Database does not pluralize table names
        modelBuilder.Conventions
            .Remove<PluralizingTableNameConvention>();
    }
}

EDIT: So the question is, just like Linq-to-Sql, does Linq-to-Sqlite provider allow us to create tables in the database equivalent to the models created using the code-first approach ? 编辑:所以问题是,就像Linq-to-Sql一样,Linq-to-Sqlite提供程序是否允许我们在数据库中创建与使用代码优先方法创建的模型等效的表?

Entity Framework Code First with SQLite only Works with an existing database, so no. 带有SQLite的Entity Framework Code First仅适用于现有数据库,因此不能。 There are alternative EF providers for sqlite that do support database creation with Code First... Devart for example sqlite有其他EF提供程序,它们确实支持使用Code First ... Devart创建数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM