【问题标题】:I can't create a simple dataBase in EntityFramework Core我无法在 EntityFramework Core 中创建简单的数据库
【发布时间】:2017-12-07 22:10:55
【问题描述】:

我是 EntityFramework 和 Core 的新手。

我想在 EntityFramework Core 2.0 中创建一个简单的数据库。该项目是一个 ConsoleApplication,我将它包含在 Nuget 包中的 Microsoft.AspNetCore.All 中。这是一个代码:

namespace TestEF
{
class Program
{
    static void Main(string[] args)
    {
        string connStr = "Server=(localdb)\\mssqllocaldb;Database=SchoolDataBase;Trusted_Connection=True;MultipleActiveResultSets=true";

        var db = new SchoolContext(SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connStr).Options);
        db.Students.Add(
            new Student()
            {
                LastName = "Miranda",
                EnrollmentDate = DateTime.Now,
                FirstMidName = "Guillermo"
            });
        db.SaveChanges();

        Console.WriteLine("Hello World!");
        Console.ReadLine();
    }
}

public class Student
{
    public int ID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
    public DateTime EnrollmentDate { get; set; }
}

public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions options) : base(options)
    {
    }
    public DbSet<Student> Students { get; set; }
}
}

我正在使用用户名“gmiranda”登录内部网。当我运行项目时,我收到以下消息错误:

发生System.Data.SqlClient.SqlException H结果=0x80131904 Message=无法打开登录请求的数据库“SchoolDataBase”。登录失败。 用户 'domain\gmiranda' 登录失败。

我做错了什么? 谢谢!

【问题讨论】:

    标签: entity-framework-core


    【解决方案1】:

    你应该试试这个代码:

    class Program
    {
        static void Main(string[] args)
        {
            string connStr = "Data Source=(localdb)\\mssqllocaldb;Database=SchoolDataBase;Trusted_Connection=True;MultipleActiveResultSets=true";
    
            var db = new SchoolContext();
            db.Students.Add(
                new Student()
                {
                    LastName = "Miranda",
                    EnrollmentDate = DateTime.Now,
                    FirstMidName = "Guillermo"
                });
            db.SaveChanges();
    
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
    
    public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }
    }
    
    public class SchoolContext : DbContext
    {
        public SchoolContext() : base()
        {
    
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=SchoolDataBase;Trusted_Connection=True;MultipleActiveResultSets=true");
        }
        public DbSet<Student> Students { get; set; }
    }
    

    然后打开包管理器控制台并使用命令 Add-Migration FirstMigrarion。 它将迁移添加到名为 FirsMigration 的数据库中。 然后使用命令更新数据库。 然后运行你的程序。

    尝试使用本教程:https://docs.microsoft.com/en-us/ef/core/get-started/full-dotnet/new-db

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 2021-07-03
      • 2012-04-09
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      相关资源
      最近更新 更多