Entity Framework Null Reference Exception

折月煮酒 提交于 2020-01-06 09:06:35

问题


I am trying to grasp EF Code First but I still do not get how to access the referenced objects from another class (due to lack of enough knowledge, I cannot even formulate the question).

Here's what my simple code looks like:

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

I get a NullReferenceException on the line I try to write the destination name to the console.

Thanks in advance.


回答1:


Try using navigation properties:

First make Destination virtual

public virtual Destination Destination { get; set; }

Then use Include method

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



回答2:


Just set Destination property in your Lodging class, virtual. This tell, the EF to load Destination automatically when you need it(Lazy Loading). So your Lodging class should looks like:

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


来源:https://stackoverflow.com/questions/25962113/entity-framework-null-reference-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!