Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF7 (Code First) + SQLite doesn't create a database and the tables for the models

Im currently trying to recreate the example, done in the documentation http://ef.readthedocs.org/en/latest/getting-started/uwp.html , using EF7 and SQLite to create a Universal Windows Platform app.

I've installed the required EF7 and EF7 Commands package, and created the model and context:

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

My problem now is, that after building the solution, the command that should scaffold a migration to create the initial set of tables for my model fails with the following exception:

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.

Does anyone have a solution for this problem ? Thanks in advance

like image 726
fhin Avatar asked Dec 30 '15 16:12

fhin


Video Answer


1 Answers

In my case the solution to the problem was to create the database and tables through code in the app.xaml, before the app starts.

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

Context + Model:

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

Additionally before accessing the database, i ensure that its created, e.g.

 using (var db = new BloggingContext())
 {
     db.Database.EnsureCreated();
     Blogs.ItemsSource = db.Blogs.ToList();
 }
like image 185
fhin Avatar answered Sep 28 '22 15:09

fhin