简体   繁体   English

实体框架教程(简单的代码优先示例)不起作用

[英]Entity Framework tutorial (simple code-first example) doesn't work

Today I started following an Entity Framework tutorial: Simple Code First Example 今天,我开始关注Entity Framework教程: 简单代码优先示例

I think I have everything how it should be, but my application doesn't work. 我想我拥有应有的一切,但我的应用程序无法正常工作。

Here's my code: 这是我的代码:

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.");
        }
    }
}

It builds without any errors or warnings, console starts, but it doesn't go past 它的构建没有任何错误或警告,可以启动控制台,但是它不会过去

ctx.Students.Add(stud);

It also didn't create any database and tables. 它还没有创建任何数据库和表。

I followed everything from the tutorial, and have no idea why it doesn't work. 我遵循了教程中的所有内容,却不知道为什么它不起作用。

EDIT: It does throw an errorafter 60 seconds (Additional information are in polish, I don't really now how to change it to english in VS): An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll 编辑:它确实在60秒后引发错误(其他信息以波兰语显示,我现在真的不怎么在VS中将其更改为英语):EntityFramework中发生了类型为'System.Data.SqlClient.SqlException'的未处理异常。 dll文件

Additional information: Wystąpił błąd związany z siecią lub wystąpieniem podczas ustanawiania połączenia z serwerem programu SQL Server. 附加信息:SQL Server程序。SQL Server。 Nie można odnaleźć serwera lub jest on niedostępny. Niemożnaodnaleźćserwera lub jest onniedostępny。 Sprawdź, czy nazwa wystąpienia jest poprawna i czy konfiguracja serwera programu SQL Server zezwala na połączenia zdalne. SQL Server的SQL Server编程。 (provider: SQL Network Interfaces, error: 26 - Błąd podczas lokalizowania określonego serwera/wystąpienia) (提供者:SQL网络接口,错误:26-Błądpodczas lokalizowaniaokreślonegoserwera /wystąpienia)

And I have to add, I'm using Visual Studio 2013 Community and the localDB which is included in this version of visual studio. 而且我必须添加,我使用的是Visual Studio 2013社区和此版本的Visual Studio中包含的localDB。

Did you try to make your navigation properties as virtual? 您是否尝试过将导航属性设置为虚拟? Also if you want to connect this two Student and Standard class to each other you need to define foreign keys. 另外,如果要将这两个Student类和Standard类相互连接,则需要定义外键。

For example add this in Student class: 例如,将其添加到Student类中:

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
}

And this in Stadart class: 在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; }
}

Hope this will help you 希望这个能对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM