C# 实体框架教程(简单代码第一个示例)没有';行不通

C# 实体框架教程(简单代码第一个示例)没有';行不通,c#,entity-framework,C#,Entity Framework,今天,我开始学习实体框架教程: 我想我已经掌握了一切,但我的应用程序不起作用 这是我的密码: public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } public DateTime DateOfBirth { get; set; } public byte[]

今天,我开始学习实体框架教程:

我想我已经掌握了一切,但我的应用程序不起作用

这是我的密码:

public class Student
{
    public Student()
    {

    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[] Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }

    public Standard Standard { get; set; }
}

public class Standard
    {
        public Standard()
        {

        }
        public int StandardId { get; set; }
        public string StandardName { get; set; }

        public ICollection<Student> Students { get; set; }
    }

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

    }

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

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();
            Console.WriteLine("done.");
        }
    }
}
它也没有创建任何数据库和表

我遵循了教程中的所有内容,不知道为什么它不起作用

编辑: 60秒后它确实会抛出一个错误(其他信息是波兰语的,我现在真的不知道如何在VS中将其更改为英语): EntityFramework.dll中发生类型为“System.Data.SqlClient.SqlException”的未处理异常

其他信息:Wystąpiąbąd związany z sieciąlub WystąPienim podczas ustanawiania poąączenia z serwerem programu SQL Server。Nie można odnaleźćserwera lub与niedostępny开玩笑。Sprawdź,czy nazwa wystąpienia与poprawna和czy konfiguracja serwera编程SQL Server zezwala na poączenia zdalne开玩笑。(提供者:SQL网络接口,错误:26-Błd podczas lokalizowania określonego serwera/wystąpienia)


我必须补充一点,我使用的是Visual Studio 2013社区和此版本的Visual Studio中包含的localDB。

您是否尝试将导航属性设置为虚拟属性?此外,如果要将这两个学生类和标准类相互连接,则需要定义外键

例如,在学生课堂上添加以下内容:

public class Student
{
    public Student()
    {

    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[] Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }

    public int StandartID{get;set;}//foreign key references Standard(StandardId)
    public virtual Standard Standard { get; set; }//navigation property
}
在Stadart类中:

public class Standard
{
    public Standard()
    {

    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }

    public int StudentID{get;set;}//foreign key references Student(StudentID)
    public virtual ICollection<Student> Students { get; set; }
}
公共类标准
{
公共标准()
{
}
公共int标准ID{get;set;}
公共字符串StandardName{get;set;}
public int StudentID{get;set;}//外键引用Student(StudentID)
公共虚拟ICollection学生{get;set;}
}

希望这能帮助您

我看不到您的连接字符串,您必须在您的上下文中提供它。对不起,链接现在可以工作了。在本教程中,并没有关于连接字符串的内容,请检查它。“并没有超过…”。这到底是什么意思?是否有错误消息?如果是这样的话,了解它是什么会有帮助…@SaeedHamed,如果默认设置对您来说足够好,那么就不需要连接字符串,并且本教程有意使用默认设置。至于你的链接,我很抱歉,但我真的不认为这有什么帮助。
public class Standard
{
    public Standard()
    {

    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }

    public int StudentID{get;set;}//foreign key references Student(StudentID)
    public virtual ICollection<Student> Students { get; set; }
}