В учебнике написано что при lazy loading'е при первом обращении к свойству оно подгружается из БД. Но вот я лично получал вместо нужного мне объекта NullReferenceException
namespace ConsoleApplication19
{
class Team
{
public int Id { get; set; }
public string Name { get; set; }
public string Coach { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
class Player
{
public int Id { get; set; }
public string Name { get; set; }
public string SecondName { get; set; }
public int Age { get; set; }
public bool Active { get; set; }
[ForeignKey("Team")]
public int TeamId { get; set; }
public Team Team { get; set; }
}
class TeamContext : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Team> Teams { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (TeamContext db = new TeamContext())
{
var Teams = db.Teams.Where(x => x.Name == "Bovaria").ToList();
foreach (var team in Teams)
{
foreach (var Player in team.Players)
{
Console.WriteLine(Player.Name);
}
}
}
}
}
}
Строка подключения
<connectionStrings>
<add name="TeamContext" connectionString="data source=(localdb)\v11.0;Initial Catalog=Teamstest;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
как тогда мне подгрузить именно при помощи "Ленивой загрузки"?