8

Is There Any Way To Implement Entity Framework Core In Full .Net Framework Console Application?

mohammad kamali
  • 409
  • 4
  • 12
  • AFAIK EF Core supports .NET Core 1.0.1+ (or it was 1.0?) and .NET Framework 4.5.1+... – Adriano Repetti Nov 21 '16 at 09:25
  • Possible duplicate of: http://stackoverflow.com/questions/13385013/how-to-add-entity-framework-to-console-application-images-are-included – NikxDa Nov 21 '16 at 09:26
  • you can use ef core using asp.net core with full .net framework, so I think it's something possible – Ali Khalili Nov 21 '16 at 09:29
  • yes,you can.here is the doc : https://learn.microsoft.com/en-us/ef/core/get-started/full-dotnet/new-db – Sampath Nov 21 '16 at 10:13
  • @Sampath The link you have provided is for using `EF Core` on `.NET Core` (not on Full .NET Framework app). – nam May 23 '20 at 16:37
  • @NikxDa How the [link](https://stackoverflow.com/q/13385013/1232087) you provided is related to `EF Core`? That link is about using `EF` and not `EF Core`. – nam May 23 '20 at 16:41
  • @nam This thread is 3.5 years old, so I am not sure that it matters a lot anymore – NikxDa May 23 '20 at 21:21

1 Answers1

5

First you need to create console application with full .net framework, Second install these packages using package manager console,

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools –Pre

Now you need to create your model and context

namespace ConsoleEfCore
{
    class Program
    {
        static void Main(string[] args)
        {
            MyContext db = new MyContext();
            db.Users.Add(new User { Name = "Ali" });
            db.SaveChanges();
        }
    }
    public class MyContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=.;Database=TestDb;Trusted_Connection=True;");
        }
    }
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

then just need to use this command

Add-Migration initial

and then you need to update your database to create that

Update-Database

run project and you we'll see User would insert to your database

Ali Khalili
  • 210
  • 3
  • 14
  • 2
    It doesn't work for me. install-package. I got this error : `Could not install package 'Microsoft.EntityFrameworkCore.SqlServer 2.1.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.` – Jaider May 31 '18 at 17:39
  • it seems `Microsoft.EntityFrameworkCore.SqlServer` 1.1.5 or lower support .NET 4.5+, but the EF 2.0.0+ is not supported. – Jaider May 31 '18 at 17:43
  • In the example code, the connection string is hard-coded. Is there a way to direct EFCore to read from a configuration file when creating a migration? – Little Endian Mar 02 '20 at 17:15
  • 1
    EF-Core 3x is compiled based on .NET Standard 2.0 and is compatible with .NET 4.8. But EF-Core 5x is compiled based on .NET Standard 2.1 and it's not compatible with .NET 4x anymore: https://github.com/dotnet/standard/issues/859 – VahidN Dec 24 '20 at 08:29