I am relatively new to .NET and have recently 'upgraded' my application (started a new project and copied the files over) from EntityFramework 6.4.4 to EntityFramework core 7.0.7, and similarly .NET 4.8 to .NET 7.0.
For the application, there are two classes I have defined; Accounts and Sites. Each Site belongs to an Account and each Account owns a list of Sites. The Account class has an attribute of type ICollection<Site>
to contain all of the Sites belonging to a particular Account. Similarly, the Site class has an Account attribute for the Account that owns that Site.
public class Account
{
[Key][StringLength(50)] public string AccountId { get; set; }
public ICollection<Site> Sites { get; set; }
}
public class Site
{
[Key] public int SiteId { get; set; }
public string AccountId { get; set; } = "";
public Account Account { get; set; } = new Account();
}
Either than the change in frameworks, the only difference between the new and old applications is that the Account and AccountId attributes in the Site class are explicitly declared in the new app...
Old app:
public string Account { get; set; }
public string AccountId { get; set; }
New app:
public string Account { get; set; } = new Account();
public string AccountId { get; set; } = "";
Now, I am trying to write a function that returns all of the Sites for a given Account, or returns all of the Sites in the DB if no Account is specified, while including the reference to the owner Account. Note that "Sites" is a DbSet<Site> containing all of the Sites in the DB.
public List<Site> GetSites(string? AccountName = null){
if(AccountName == null)
return Sites.Include(x => x.Account).ToList(); //problem here
else{
var account = GetAccount(AccountName);
return Sites.Where(x => x.AccountId == account.AccountId).Include(x => x.Account).ToList();
}
}
There is no problem when specifying the AccountName, however, when GetSites() is run to return all of the Sites in the DB, the updated application reports: "System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values." The exception appears to be thrown at the third line:
Sites.Include(x => x.Account).ToList()
Again, everything works as expected on the old application, I am only running into problems with the updated frameworks.
I realize that I can probably write a lengthy for loop to simulate the Include function by looping through all of the Sites and properly setting the Accounts, however, I am trying to see if there is a better way through Linq commands.
Any ideas or potential fixes would be much appreciated. I would be happy to supply more information if required.
Thanks!
I have tried replacing the troublesome Include() line with the following to no avail...
Sites.Include("Account").ToList(); //Same error as before
Sites.Where(x => x.Account != null).Include(x => x.Account).ToList(); //Same error
(from s in Sites join a in Accounts on s.Account equals a select s).ToList(); //sketchy
The hardcoded (sketchy) query did not result in an exception or error and appeared to work but my mentor advised that it would bypass the entity framework and would not update the object model.