Wednesday, September 14, 2016

Entity Framework Core Inside Class Library & Entity Framework Core DB Migration Inside Class Library

We are working in a project lately using .Net Core .

And Sure we separate our project into some layers , one of them is Domain Entities as separate class library using Entity Framework Core V 1.0.0 .

- We encounter some problems one of them is officially mentioned here :
"Could not invoke this command on the startup project '(your project name)'. This preview of Entity Framework tools does not support commands on class library projects in ASP.NET Core and .NET Core applications."
https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html#targeting-class-library-projects-is-not-supported

- And When you try to add "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"  inside  project.json "dependencies" you will get :
----------------------
Package Microsoft.EntityFrameworkCore.Design 1.0.0-preview2-final is not compatible with netstandard1.6 (.NETStandard,Version=v1.6). Package Microsoft.EntityFrameworkCore.Design 1.0.0-preview2-final supports:
      - net451 (.NETFramework,Version=v4.5.1)
      - netcore50 (.NETCore,Version=v5.0)
      - netcoreapp1.0 (.NETCoreApp,Version=v1.0)
    One or more packages are incompatible with .NETStandard,Version=v1.6.
---------------------

The complete Solution:

1 - Inside your Entity Class library Project open "project.json" file and replace with the following  :

-------------------
{
  "title": "Entities",
  "version": "1.0.0-*",

  "buildOptions": {
    "emitEntryPoint": true
  },
  "frameworks": {
    "netcoreapp1.0": {}
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final"
    }
  }
}
-------------------

2- Add you app start Point by adding new Class Library called "Program.cs" To your Entity Class library Project Contains :

-------------------
namespace EntityProject
{
    public class Program
    {
        public static void Main(string[] args)
        {
        }
    }
}
-------------------

3- Database Migration:
     A- Inside your Entity Class library Project Add "Startup.cs" File Contains :
-------------------
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace EntityProject
{
    public class Startup
    {    
        public void ConfigureServices(IServiceCollection services)
        {
            var connection = @"Persist Security Info=True;Initial Catalog=DBForTest;Password=XX;User ID=XXX;Data Source=XXX";
            services.AddDbContext<DSContext>(options => options.UseSqlServer(connection));            
        }
    }
}
-------------------
     B- Using Package Manager Console :
-------------------
PM> Add-Migration MyDBMigration1
PM> Update-Database
-------------------

4-  Inside The Project that use the Entities Class Library DLL , Be sure you add the connection string and register your Repository

5- You will Find the complete Solution Source Code @github/naadydev :
https://github.com/naadydev/Entity-Framework-Core-Inside-Class-Library



Twitter  /  Facebook  /  GitHub ->  @naadydev




No comments: