完整.Net中的实体框架核心? [英] Entity Framework Core in Full .Net?

查看:115
本文介绍了完整.Net中的实体框架核心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在完整的.Net Framework控制台应用程序中实现实体框架核心?

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

推荐答案

首先,您需要创建带有完整.net框架的控制台应用程序,
然后,使用软件包管理器控制台安装这些软件包。

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; }
    }
}

然后只需要使用此命令

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

这篇关于完整.Net中的实体框架核心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆