Entity Framework Core in Full .Net?

前端 未结 1 563
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 11:57

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

相关标签:
1条回答
  • 2021-01-05 12:49

    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

    0 讨论(0)
提交回复
热议问题