C# 实体框架空引用异常

C# 实体框架空引用异常,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我试图先掌握EF代码,但我仍然不知道如何从另一个类访问引用的对象(由于缺乏足够的知识,我甚至无法表达这个问题) 下面是我的简单代码: public class Destination { public int DestinationId { get; set; } public string Name { get; set; } public string Country { get; set; } public string Description { get;

我试图先掌握EF代码,但我仍然不知道如何从另一个类访问引用的对象(由于缺乏足够的知识,我甚至无法表达这个问题)

下面是我的简单代码:

public class Destination
{
    public int DestinationId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public string Description { get; set; }

    public byte[] Photo { get; set; }

    public List<Lodging> Lodgings { get; set; }
}
public class Lodging
{
    public int LodgingId { get; set; }

    public string Name { get; set; }
    public string Owner { get; set; }
    public bool IsResort { get; set; }

    public Destination Destination { get; set; }
}
public class BreakAwayContext: DbContext
{
    public DbSet<Destination> Destinations { get; set; }
    public DbSet<Lodging> Lodgings { get; set; }
}
private static void InsertDestination()
    {
        var destination = new Destination
        {
            Country = "Indonesia",
            Description = "EcoTourism at its best in exquisite Bali",
            Name = "Bali"
        };
        using(var context = new BreakAwayContext())
        {
            context.Destinations.Add(destination);
            context.SaveChanges();
        }
    }

    private static void InsertLodging()
    {
        var lodging = new Lodging()
        {
            Name = "x",
            IsResort = false,
            Owner = "asdasd"
        };
        using(var context = new BreakAwayContext())
        {
            var dest = context.Destinations.Find(1);
            lodging.Destination = dest;
            context.Lodgings.Add(lodging);
            context.SaveChanges();
        }
    }
    private static void ShowLodgings()
    {
        using(var context = new BreakAwayContext())
        {
            foreach(var l in context.Lodgings)
            {
                Console.WriteLine("{0} {1} {2}", l.Name, l.Owner, l.Destination.Name);
            }
        }
    }
public class Lodging
{
    public int LodgingId { get; set; }

    public string Name { get; set; }
    public string Owner { get; set; }
    public bool IsResort { get; set; }

    public virtual Destination Destination { get; set; }
}
公共类目的地
{
public int DestinationId{get;set;}
公共字符串名称{get;set;}
公共字符串国家{get;set;}
公共字符串说明{get;set;}
公共字节[]Photo{get;set;}
公共列表住宿{get;set;}
}
公务舱住宿
{
public int lodingId{get;set;}
公共字符串名称{get;set;}
公共字符串所有者{get;set;}
公共bool IsResort{get;set;}
公共目的地{get;set;}
}
公共类BreakAwayContext:DbContext
{
公共数据库集目的地{get;set;}
公共数据库集住宿{get;set;}
}
私有静态void InsertDestination()
{
var destination=新目的地
{
Country=“印度尼西亚”,
Description=“巴厘岛最佳生态旅游”,
Name=“巴厘岛”
};
使用(var context=new BreakAwayContext())
{
context.Destinations.Add(destination);
SaveChanges();
}
}
私有静态void insertlopping()
{
var住宿=新住宿()
{
Name=“x”,
IsResort=false,
Owner=“asdasd”
};
使用(var context=new BreakAwayContext())
{
var dest=context.Destinations.Find(1);
住宿。目的地=目的地;
上下文。住宿。添加(住宿);
SaveChanges();
}
}
私人静态空间展示住宿()
{
使用(var context=new BreakAwayContext())
{
foreach(上下文中的变量l.住宿)
{
WriteLine(“{0}{1}{2}”,l.Name,l.Owner,l.Destination.Name);
}
}
}
我在尝试将目标名称写入控制台的行上得到一个NullReferenceException

提前感谢。

尝试使用:

首先使
目的地
虚拟

然后使用
Include
方法

foreach(var l in context.Lodgings.Include(x => x.Destination))

只需在
住宿
课程中设置
目的地
属性,
虚拟
。这告诉我们,EF将在需要时自动加载<代码>目的地(延迟加载)。 因此,您的
住宿
课程应该如下所示:

public class Destination
{
    public int DestinationId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public string Description { get; set; }

    public byte[] Photo { get; set; }

    public List<Lodging> Lodgings { get; set; }
}
public class Lodging
{
    public int LodgingId { get; set; }

    public string Name { get; set; }
    public string Owner { get; set; }
    public bool IsResort { get; set; }

    public Destination Destination { get; set; }
}
public class BreakAwayContext: DbContext
{
    public DbSet<Destination> Destinations { get; set; }
    public DbSet<Lodging> Lodgings { get; set; }
}
private static void InsertDestination()
    {
        var destination = new Destination
        {
            Country = "Indonesia",
            Description = "EcoTourism at its best in exquisite Bali",
            Name = "Bali"
        };
        using(var context = new BreakAwayContext())
        {
            context.Destinations.Add(destination);
            context.SaveChanges();
        }
    }

    private static void InsertLodging()
    {
        var lodging = new Lodging()
        {
            Name = "x",
            IsResort = false,
            Owner = "asdasd"
        };
        using(var context = new BreakAwayContext())
        {
            var dest = context.Destinations.Find(1);
            lodging.Destination = dest;
            context.Lodgings.Add(lodging);
            context.SaveChanges();
        }
    }
    private static void ShowLodgings()
    {
        using(var context = new BreakAwayContext())
        {
            foreach(var l in context.Lodgings)
            {
                Console.WriteLine("{0} {1} {2}", l.Name, l.Owner, l.Destination.Name);
            }
        }
    }
public class Lodging
{
    public int LodgingId { get; set; }

    public string Name { get; set; }
    public string Owner { get; set; }
    public bool IsResort { get; set; }

    public virtual Destination Destination { get; set; }
}

从性能角度来看,
Include
可能是最好的,但是在您创建了
目标
虚拟
之后,它将被延迟加载。您的
目标
不会被加载。您需要启用延迟加载或使用即时加载。请参阅:。顺便说一下,您已经选择了正确的书籍来学习代码:-)