完整.Net中的实体框架核心?

moh*_*ali 3 .net entity-framework-core

有什么方法可以在完整的.Net Framework控制台应用程序中实现实体框架核心?

小智 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
Run Code Online (Sandbox Code Playgroud)

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

then just need to use this command

Add-Migration initial
Run Code Online (Sandbox Code Playgroud)

and then you need to update your database to create that

Update-Database
Run Code Online (Sandbox Code Playgroud)

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

  • 它对我不起作用。安装包。我收到此错误:`无法安装包'Microsoft.EntityFrameworkCore.SqlServer 2.1.0'。您正在尝试将此包安装到以“.NETFramework,Version=v4.6”为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件。有关更多信息,请联系软件包作者。` (2认同)
  • EF-Core 3x 基于.NET Standard 2.0 编译,兼容.NET 4.8。但 EF-Core 5x 是基于 .NET Standard 2.1 编译的,不再兼容 .NET 4x:https://github.com/dotnet/standard/issues/859 (2认同)