sqlite - EF7 (Code First) + SQLite 不为模型创建数据库和表

标签 sqlite win-universal-app entity-framework-core

我目前正在尝试重新创建示例,在文档中完成 http://ef.readthedocs.org/en/latest/getting-started/uwp.html ,使用 EF7 和 SQLite 创建通用 Windows 平台应用。

我已经安装了所需的 EF7 和 EF7 命令包,并创建了模型和上下文:

 public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
            string dirPath = ApplicationData.Current.LocalFolder.Path;
            string connectionString = "Filename=" + Path.Combine(dirPath, "blogging.db");
            optionsBuilder.UseSqlite(connectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

我现在的问题是,在构建解决方案之后,为我的模型创建初始表集的迁移支架命令失败,出现以下异常:

PM> Add-Migration MyFirstMigration
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
 at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
 at System.Reflection.RuntimeAssembly.get_DefinedTypes()
 at Microsoft.Data.Entity.Design.Internal.StartupInvoker..ctor(String startupAssemblyName, String environment)
 at Microsoft.Data.Entity.Design.DbContextOperations..ctor(ILoggerProvider loggerProvider, String assemblyName, String startupAssemblyName, String environment)
 at Microsoft.Data.Entity.Design.MigrationsOperations..ctor(ILoggerProvider loggerProvider, String assemblyName, String startupAssemblyName, String environment, String projectDir, String rootNamespace)
 at Microsoft.Data.Entity.Design.OperationExecutor.<>c__DisplayClass3_0.<.ctor>b__3()
 at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
 at Microsoft.Data.Entity.Design.OperationExecutor.<AddMigrationImpl>d__7.MoveNext()
 at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
 at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
 at Microsoft.Data.Entity.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
 at Microsoft.Data.Entity.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

有人能解决这个问题吗? 提前致谢

最佳答案

在我的例子中,问题的解决方案是在应用程序启动之前通过 app.xaml 中的代码创建数据库和表。

using (var db = new BloggingContext())
{
    db.Database.EnsureCreated();
    db.Database.Migrate();
}

上下文 + 模型:

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string path = ApplicationData.Current.LocalFolder.Path;
        if (!File.Exists(Path.Combine(path, "blogging.db")))
        {
            File.Create(Path.Combine(path, "blogging.db"));
        }
        optionsBuilder.UseSqlite("Data Source=" + Path.Combine(path, "blogging.db")+";");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Make Blog.Url required
        modelBuilder.Entity<Blog>()
            .Property(b => b.Url)
            .IsRequired();
    }
}

[Table("Blog")]
public class Blog
{
    [Key]
    public int BlogId { get; set; }
    [MaxLength(100)]
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

[Table("Post")]
public class Post
{
    [Key]
    public int PostId { get; set; }

    [MaxLength(30)]
    public string Title { get; set; }
    [MaxLength(250)]
    public string Content { get; set; }

    public int BlogId { get; set; }
    [ForeignKey("BlogId")]
    public Blog Blog { get; set; }
}

此外,在访问数据库之前,我确保它已创建,例如

 using (var db = new BloggingContext())
 {
     db.Database.EnsureCreated();
     Blogs.ItemsSource = db.Blogs.ToList();
 }

关于sqlite - EF7 (Code First) + SQLite 不为模型创建数据库和表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532940/

相关文章:

sqlite - 使用Sqlite Flutter BETWEEN查询返回最近2天的记录

windows-phone-8 - Windows Phone 8 ApplicationSettings - 在通用应用程序中获取设置

c# - 如何将 .net 核心 EF 注入(inject) WPF 应用程序

UWP标题栏关闭按钮大小

c# - UWP - 未找到包含提供的指纹的证书

c# - 在 EF Core 2.1 中使用环境事务时是否需要手动关闭 DbConnection?

include - 无法调用Entity Framework 7中的include方法

django - 在使用syncdb时如何使用Django来呈现旧数据库而不破坏它?

java - 将大量 JSON 数据插入 android 中的 SQLite 数据库的方法是什么

android - 你能删除 SQLite 数据库中的列吗?