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

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

问题描述

有没有办法在完整的 .Net Framework 控制台应用程序中实现 Entity Framework Core?

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

然后只需要使用这个命令

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

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

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