0

I've been playing around with Entity Framework for a while now. For some reason however the foreign keys all seem to be null untill I explicitely use the Include method to include them.

According to what I've read here: Entity Framework 4.1 Virtual Properties by making them virtual they should be lazy loaded (and not null).

This is my model:

class AltiGoalInfo
{
    public int AltiGoalInfoId { get; set; }
    public int ShotTime { get; set; } //Time it took from shooting till scoring
    public virtual AltiPlayerInfo AltiPlayerInfo { get; set; }
    public virtual AltiTeamEnum Team { get; set; }

    public override string ToString()
    {
        double shotTime = ShotTime;
        shotTime /= 1000;
        shotTime = Math.Round(shotTime, 2);
        return shotTime + " by " + AltiPlayerInfo;
    }
}



class AltiPlayerInfo
{
    public int AltiPlayerInfoId { get; set; }
    public String VaporId { get; set; }

    public String LastKnownNickname { get; set; }
    public int LastKnownLevel { get; set; }
    public int LastKnownAceRank { get; set; }
    public Boolean LastKnownDemo { get; set; }

    private IList<String> _knownIps = new List<String>();
    public IList<String> KnownIps
    {
        get
        {
            return _knownIps;
        }
        set
        {
            _knownIps = value;
        }
    }

    public string KnownIpsString
    {
        get
        {
            return String.Join(";", _knownIps);
        }
        set
        {
            _knownIps = value.Split(';').ToList();
        }
    }

    public int Goals { get; set; }
    public int MatchesPlayed { get; set; }
    public int MatchesWon { get; set; }

    public double CalcWinRate()
    {
        double mwon = MatchesWon;
        double mplay = MatchesPlayed;
        double result = mwon / mplay * 100.0;
        return Math.Round(result, 0);
    }

    public DateTime FirstSeen { get; set; }
    public DateTime LastSeen { get; set; }

    public override string ToString()
    {
        return LastKnownNickname;
    }
}



class AltiDbContext : DbContext
{
    public DbSet<AltiPlayerInfo> AltiPlayerInfos { get; set; }
    public DbSet<AltiGoalInfo> AltiGoalInfos { get; set; }
}

When I use the following code to acces the data the brrr variable is null:

var test = altiDbContext.AltiGoalInfos.First();
var brrr = test.AltiPlayerInfo;

When I however include in manually first, it does work:

var test = altiDbContext.AltiGoalInfos.Include(t => t.AltiPlayerInfo).First();
var brrr = test.AltiPlayerInfo;

Edit: I've also verified the configuration of the DbContext, by default all settings in the configuration are set to true (which should enable lazy loading).

Community
  • 1
  • 1
Devedse
  • 1,801
  • 1
  • 19
  • 33

1 Answers1

0

I found the solution, I had to make the model classes public

Devedse
  • 1,801
  • 1
  • 19
  • 33