8,497
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeFirst
{
class Program
{
static void Main(string[] args)
{
using (var db = new StudentContext())
{
var student = new Student() { Name = "Fabio Scopel" };
var mathSubj = new Subject() { Name = "Mathetics" };
var scienceSubj = new Subject() { Name = "Data Structures" };
student.Subjects.Add(mathSubj);
student.Subjects.Add(scienceSubj);
db.Students.Add(student);
db.SaveChanges();
}
}
public class Student
{
public int studentID { get; set; }
public string Name { get; set; }
public virtual List<Subject> Subjects { get; set; }
public Student()
{
this.Subjects = new List<Subject>();
}
}
public class Subject
{
public int SubjectId { get; set; }
public string Name { get; set; }
public virtual Student Student { get; set; }
}
class StudentContext: DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Subject> Subjects { get; set; }
}
}
}
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
To:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="StudentContext" connectionString ="Server=Erik-work;Database=StudentDatabase;user=sa;password="*******" providerName="System.Data.SqlClient"/>
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
上面除了密码我用*******代替外,其他一直就是原程序。在视频中,作者在其中加了一个<connectionStrings></connectionStrings>。我是完全按照视频上来的,不过视频上用的是visual studio 2010(大概),我用的是visual studio 2013,我在写代码时发现一些在使用entity framework 时的重要命令不一样。
那到底问题出在哪里呢?谢谢大神们