使用Entity Framework 通过code来建表和输入数据,但总是显示错误,请大神来看看那里出错

歪着看世界 2014-09-15 06:32:24
最近自学C#,其中Entity Framework 是一个重点,所以找了一个视频,国内的找不到又讲解又上代码的视频,所以只能找了一个youtube上老外的视频,地址 https://www.youtube.com/watch?v=HbDOhCjjxSY
视频讲的是通过code在SQL server 里建立表格并输入数据,下面是我按照视频写的代码:
基本情况:视频中使用 Entity Framework 5.0 ,我是使用 6.0;
建立的是Visual C# 下的Console Application.
下面是代码:


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; }
}
}
}


视频中该程序是顺利成功了,但这段代码在我机子上运行时,先出现一个黑色Console 窗口,但同时代码窗口中出现出错信息,总是db.Students.Add(student); 这句话出错,截图为:

主要说是:
An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: Failed to set Database.DefaultConnectionFactory to an instance of the 'System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework' type as specified in the application configuration. See inner exception for details.

请大神们指教指教,到底哪里出错了呢?那到底怎么改呢?
...全文
1841 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
歪着看世界 2016-04-05
  • 打赏
  • 举报
回复
好久之前的问题,感谢各位网友的参与和帮助,我就基本按照平均给分,有内容的网友就稍微多给点。
save4me 2014-09-15
  • 打赏
  • 举报
回复
参考: Entity Framework > 开始操作 > 配置文件设置
引用
如果您不设置默认连接工厂,则 Code First 将使用指向 .\SQLEXPRESS 的 SqlConnectionFactory
在视频里面,entityFramework节点没有providers子节点,而你的配置文件里面有。你把它删除看看。 另外因为你设置了connectionStrings,所以如果上面的操作不行,你把整个entityFramework删除,看看是否还报错,报什么错。
引用 4 楼 u011552243 的回复:
app.config 代码为:

<?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 时的重要命令不一样。 那到底问题出在哪里呢?谢谢大神们
save4me 2014-09-15
  • 打赏
  • 举报
回复
你用的数据库连接是什么?视频里面用的数据库连接又是什么? 如果你使用的是SQL Server数据库,可以参考下面的链接修改app.config Changing EF's default connection factory from LocalDb to Sql Server
引用 楼主 u011552243 的回复:
主要说是: An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll Additional information: Failed to set Database.DefaultConnectionFactory to an instance of the 'System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework' type as specified in the application configuration. See inner exception for details. 请大神们指教指教,到底哪里出错了呢?那到底怎么改呢?
q107770540 2014-09-15
  • 打赏
  • 举报
回复
Change
<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>
敌敌畏耶 2014-09-15
  • 打赏
  • 举报
回复
看下你的数据库连接串···app.config里面···
歪着看世界 2014-09-15
  • 打赏
  • 举报
回复
app.config 代码为:

<?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 时的重要命令不一样。 那到底问题出在哪里呢?谢谢大神们

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧