Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c实体框架对象引用异常_C#_.net_Entity Framework_Exception_Object Reference - Fatal编程技术网

C# c实体框架对象引用异常

C# c实体框架对象引用异常,c#,.net,entity-framework,exception,object-reference,C#,.net,Entity Framework,Exception,Object Reference,对于以下例外情况,我有一些困难:对象引用未设置为对象的实例 EntityFramework MVC5上的代码优先迁移 public class Driver { [Key] public int NumberId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Age { get; set; }

对于以下例外情况,我有一些困难:对象引用未设置为对象的实例

EntityFramework MVC5上的代码优先迁移

public class Driver
{   
    [Key]
    public int NumberId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }

    public string TeamName { get; set; }
    public Team Team { get; set; }

}
配置:

namespace f1app.Migrations.F1app
{
    using Data;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<f1app.Data.F1appContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            MigrationsDirectory = @"Migrations\F1app";
        }

        protected override void Seed(f1app.Data.F1appContext context)
        {

            context.Teams.AddOrUpdate(
                t => t.TeamName, DummyData.getTeams().ToArray());
            context.SaveChanges();

            context.Drivers.AddOrUpdate(
                d => new { d.FirstName, d.LastName }, DummyData.getDrivers(context).ToArray());
        }
    }
}
然后获取一些虚拟数据,以便驱动程序进入表中

public static List<Driver> getDrivers(F1appContext context)
{
    List<Driver> drivers = new List<Driver>(){
        new Driver{
            NumberId=5,
            FirstName="Sebastian",
            LastName="Vettel",
            Age="29",
            TeamName=context.Teams.Find("Ferrari").TeamName,
        },
        new Driver{
            NumberId=44,
            FirstName="Lewis",
            LastName="Hamilton",
            Age="32",
            TeamName= context.Teams.Find("Mercedes").TeamName
        },
        new Driver{
            NumberId=19,
            FirstName="Felipe",
            LastName="Massa",
            Age="36",
            TeamName=context.Teams.Find("Williams").TeamName
        }
    };
    return drivers;
}
并使用PM控制台进行更新-更新数据库-配置类型名称f1app.Migrations.f1app.Configuration。但只有团队的表格工作正常,驱动程序表格没有任何数据。。。。知道为什么吗?

你有一个错误的位置

context.SaveChanges();
即:

protected override void Seed(f1app.Data.F1appContext context)
    {

        context.Teams.AddOrUpdate(
            t => t.TeamName, DummyData.getTeams().ToArray());
        context.SaveChanges(); // <----------------------

        context.Drivers.AddOrUpdate(
            d => new { d.FirstName, d.LastName }, DummyData.getDrivers(context).ToArray());
        //should be here:
        context.SaveChanges(); // <----------------------
    }

一个就够了。我将这两种方法都用于说明。

对不起,我已经尝试过了,但没有任何改变,在运行package manager控制台命令后仍然出现相同的错误异常-对象引用未设置为对象的实例。这是我在运行update命令后从pm控制台获得的消息的结尾:。。。。。在System.Data.Entity.Migrations.Design.ToolingFacade.UpdateString targetMigration中,System.Data.Entity.Migrations.UpdateDatabaseCommand.c__在System.Data.Entity.Migrations.MigrationsDomainCommand.ExecuteAction命令对象引用处的布尔力未设置为对象的实例。我认为问题与引用或其他内容有关。。。。不知道-试图了解c和.NETMVC现在是如何工作的…对不起大家。。。。找到它-是一些小的文本错误,我做的missmash。。。。。算了吧,解决了!最初的猜测是context.Teams.FindFerrari.TeamName行由于查找未返回任何数据而失败。在上下文中是否有具有这些密钥的团队?您还可以为DummyData.getTeams添加代码吗?您的getTeams代码缺失。您的TeamName=context.Teams.FindWilliams.TeamName很奇怪。TeamName=Williams应该足够了
protected override void Seed(f1app.Data.F1appContext context)
    {

        context.Teams.AddOrUpdate(
            t => t.TeamName, DummyData.getTeams().ToArray());
        context.SaveChanges(); // <----------------------

        context.Drivers.AddOrUpdate(
            d => new { d.FirstName, d.LastName }, DummyData.getDrivers(context).ToArray());
        //should be here:
        context.SaveChanges(); // <----------------------
    }