1
votes

I want to use EF Code first migrations in Azure functions project which is class library .net 4.6.2 with EF 6.2.0. I added db context and entities. When i try to run enable-migrations or add-migration by setting this class library as default project then getting below error. I don't have any web projects in the solution.

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\xxxxx-win.nuget\packages\entityframework\6.2.0\tools\EntityFramework.psm1:720 char:5 + $domain.SetData('project', $project) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SerializationException

But when i run the project then it automatically creates __MigrationHistory table with InitialCreate entry. But i can't see this migration file anywhere in the solution.

dbcontext file for reference.

 public class TestDbContext : DbContext
    {
        public TestDbContext(string cs)
            : base(cs)
        {
        }

        public TestDbContext() :
            base(ConfigurationManager.ConnectionStrings["sql_connection"].ConnectionString)
        {
            Database.SetInitializer(new TestDBInitializer());
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TestRaw>().ToTable("TestRaw");
            modelBuilder.Entity<TestRequest>().ToTable("TestRequest");
        }

        public virtual DbSet<TestRaw> TestRaw { get; set; }

        public virtual DbSet<TestRequest> TestRequest { get; set; }

    }
    public class TestDBInitializer : CreateDatabaseIfNotExists<TestDbContext>
    {
        protected override void Seed(TestDbContext context)
        {
            base.Seed(context);
        }
    }

Also, azure functions doesn't use app.config. So not sure where EF related dependencies are registered? Can we use EF Code first migrations with Azure functions? If yes, how to see migrations folder and relevant files ? do i need to take care anything related to migrations while deploying to functionapp?

1

1 Answers

0
votes

I'll divide this answer in two parts.

  1. The error you get when trying to enable/add migrations ("Exception calling "SetData"...") could be caused by a mismatch in the target framework of the project containing your context and entities and the commands you are trying to use. Although it seems, based on your description, that you have these properly set, meaning: .net 4.6.2 and EF 6.2.0 (so neither one is .net core).

    I managed to get the same error by creating a .NET Standard 2.0 class library for my context and entities classes, installing EF 6.2 in it and running the old commands like "enable-migrations" etc.

    At Stack Overflow are some other reasons for getting the same error (pointing also to version conflicts and setting the correct project in package manager console etc).

    Creating a .NET Framework 4.6.2 class library, installing EF 6.2 and using those commands worked for me and should definitely work for you as well.

    Because you say that running the app actually runs the migrations (because you have the database initializer set) would indicate that you might simply be using the old commands when what you actually want is "dotnet ef...".

    You might want to give some more detailed information about the structure of your solution (which projects targeting which versions) to enable further analysis.

    As a sidenote, Azure Functions now support .NET Core as well, so you might want to give .NET Core a try all the way (meaning that the functions project, the class library and EF are all targetting Core).

  2. Azure Functions and EF support: there is no stopping you from using EF in Azure Functions. I just created functions with EF Code First migrations (EF 6.2) and database initializer set to migrate the db to latest version at runtime.

    I have the functions in one project and the database context and entities in another class library project, which contains an app.config file also (only needed for creating migrations or updating database manually etc). Then it is just a matter of referencing this project in the functions project to be able to use the code in there.

    Inside the function you simply create a new instance of your context class. You can get the database connection string using the basic ConfigurationManager.ConnectionStrings["foobarConnString"].ConnectionString (or the equivalent .NET Core code, if targeting Core).

    Naturally this means that you need to reference EF libraries in both the functions project and your model project.