c# - 无法看到 Entity Framework 中创建的数据库

标签 c# asp.net entity-framework

这可能是 Entity Framework 开发人员的基本问题,他们想从数据库开发人员转变为 Dot Net 开发人员。从带有 Entity Framework 模型的基本示例开始,但在编译时遇到异常。 下面是我的主要方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace EFMAin
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new SchoolContext())
            {
    Student stud = new Student() { StudentName = "New Student" };

                ctx.Students.Add(stud);
                ctx.SaveChanges();
            }
        }
    }
}

下面是我创建数据库表的类。

using ExampleCF.Models;
using ExampleCFA.Models;
using System.Collections.Generic;
namespace ExampleCF.Models
{
    public class Teacher
    {
        public int TeacherId { get; set; }
        public string TeacherName { get; set; }
        public string Address { get; set; }
        public ICollection<Student> Students { get; set; }
    }
}

namespace ExampleCFA.Models
{
    public class Student
    {
        public int StudentId { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
        public int TeacherId { get; set; }
        public Teacher Teacher { get; set; }
    }
}

下面是上下文类。 使用 System.Data.Entity;

namespace EF_Code_First_Tutorials
{

    public class SchoolContext : DbContext
    {
        public SchoolContext()
            : base()
        {

        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }

    }
}

编译此应用程序时出现以下异常。

Error 3 'System.Runtime.Remoting.Contexts.Context' does not contain a definition for 'SaveChanges' and no extension method 'SaveChanges' accepting a first argument of type 'System.Runtime.Remoting.Contexts.Context' could be found (are you missing a using directive or an assembly reference?) D:\EF\EFLab1Practice\EFCodeFirst\EFMAin\Program.cs 20 21 EFMAin

Error 2 'System.Runtime.Remoting.Contexts.Context' does not contain a definition for 'Students' and no extension method 'Students' accepting a first argument of type 'System.Runtime.Remoting.Contexts.Context' could be found (are you missing a using directive or an assembly reference?) D:\EF\EFLab1Practice\EFCodeFirst\EFMAin\Program.cs 19 21 EFMAin Error 1 'System.Runtime.Remoting.Contexts.Context': type used in a using statement must be implicitly convertible to 'System.IDisposable' D:\EF\EFLab1Practice\EFCodeFirst\EFMAin\Program.cs 15 13 EFMAin

任何人请建议这将是什么起点。

我关注了[ http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx][1]

以及要求您建议启动 Entity Framework 的最佳位置。

最佳答案

当我做这个教程时,我的数据库是在 SQL Server = (localdb)\mssqllocaldb 中创建的,尝试连接到这个数据库,然后在数据库下检查

关于c# - 无法看到 Entity Framework 中创建的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28962504/

相关文章:

java - C# Little-Endian 从 Java Big-Endian 转换

c# - 在不实现 IXmlSerializable 的情况下序列化只读集合

jquery - Bootstrap 在 IE 浏览器中的 Modal UI 问题

entity-framework - 如何在应用 SaveChanges() 之前获取实体中对象的值?

c# - 如何根据日期列获取最新信息并返回 BuildMetrics 类型的对象?

java - 从 Netbeans 中的现有实体生成表

c# - 谁能用图表解释枚举是如何存储在内存中的?

c# - LINQ to Objects 并通过索引改进性能?

asp.net - 将 session 超时增加到一周或更长时间

javascript - 带有 html table 标签的无限滚动(滚动加载)