Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Migration for Universal Windows Platform

I have following DbContext and I want to migrate it using Entity Framwork

public class TestDbContext: DbContext
{
    public DbSet<State> States { get; set; }
    public DbSet<StateType> StateTypes { get; set; }
    public DbSet<Measure> Measures { get; set; }
    public DbSet<Priority> Priorities { get; set; }
    public DbSet<Task> Tasks { get; set; }
    public DbSet<TaskType> TaskTypes { get; set; }
    public DbSet<Document> Documents { get; set; }


    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string databaseFilePath = "test.db";
        try
        {
            databaseFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, databaseFilePath);
        }
        catch (InvalidOperationException) { }
        optionsBuilder.UseSqlite($"Data source={databaseFilePath}");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

    }

I ran the statement

enable-migrations -ContextTypeName TestData.TestDbContext

in the Package Manager Console to generate the configuration. But the generation has compilation errors because following namespaces/classes can't be found:

using System.Data.Entity;
using System.Data.Entity.Migrations;
DbMigrationsConfiguration

.

namespace TestData.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<TestDatas.TestDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(TestDatas.TestDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

Any solutions on how the configuration could compile so I can add the migration?

like image 721
Simon Angerbauer Avatar asked Oct 31 '22 14:10

Simon Angerbauer


1 Answers

This documentation on EF7 may be helpful.

I tested, DbContext should use this reference:

using Microsoft.Data.Entity; 

and using System.Data.Entity.Migrations; should also changed to

using Microsoft.Data.Entity.Migrations;
like image 178
panda Avatar answered Nov 15 '22 05:11

panda