I have some entities like Patients with some relations to another entities. My problem is that, in Details view, the navigation properties (which points to other tables) does not display anything! apparently it returns null.
after a lot of search, some people says i should change some configation options in my dbContext (see this
link), but i could not find Configuration property!
some people says Lazy loading is not supported by ef core, i should include it manually in my linq query (see this
link), but also i could not find Include in my dbContext!!
Here is my Patients class :
public partial class Patients
{
[Key]
public Guid PatientRowID { get; set; }
[Required]
[StringLength(50)]
public string PatientCaseID { get; set; }
[StringLength(50)]
public string PatientFname { get; set; }
[StringLength(50)]
public string PatientLname { get; set; }
public int? SexID { get; set; }
[ForeignKey(nameof(SexID))]
[InverseProperty("Patients")]
public virtual Sex Sex { get; set; }
}
And here is my another table (Sex) :
public partial class Sex
{
public Sex()
{
Patients = new HashSet<Patients>();
Persons = new HashSet<Persons>();
}
[Key]
public int SexID { get; set; }
[StringLength(50)]
public string SexName { get; set; }
[InverseProperty("Sex")]
public virtual ICollection<Patients> Patients { get; set; }
[InverseProperty("Sex")]
public virtual ICollection<Persons> Persons { get; set; }
}
And here is my action method :
public IActionResult Details(string id)
{
if (string.IsNullOrEmpty(id))
return BadRequest();
Guid gid = Guid.Parse(id);
var patient = _dbContext.Patients.Where(p => p.PatientRowID == gid).FirstOrDefault();
if (patient == null)
return NotFound();
return View(patient);
}
And here is my Details view code :
<div>
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.PatientCaseID)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.PatientCaseID)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.PatientFname)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.PatientFname)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.PatientLname)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.PatientLname)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.SexID)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Sex.SexName) // display nothing!
</dd>
</dl>
</div>
Can anybody help me where is my problem & how to solve it?
I assume Sex can only be Male or Female? if so, Sex should be a lookup table or an enum.
public class Patient
{
public Guid PatientId { get; set; }
public string PatientCaseId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public int? SexId { get; set; }
public SexLookUp Sex { get; set; }
}
public partial class SexLookUp
{
public int SexId{ get; set; }
public string Sex { get; set; }
}
public class MvcDbDemoContext : DbContext
{
public MvcDbDemoContext (DbContextOptions<MvcDbDemoContext> options)
: base(options)
{
}
public DbSet<Patient> Patient { get; set; }
public DbSet<SexLookUp> Sex { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>().ToTable("Patients").HasKey(p => p.PatientId);
modelBuilder.Entity<Patient>().HasOne(s => s.Sex);
modelBuilder.Entity<Patient>().Property(p => p.Firstname).HasMaxLength(50);
modelBuilder.Entity<Patient>().Property(p => p.Lastname).HasMaxLength(50);
modelBuilder.Entity<SexLookUp>().ToTable("SexLookup").HasKey(s => s.SexId);
}
}
But i've misunderstood your code. I think your sample code does not solve my problem.
Your code does not work.
My code works and Patient has a foreign key constraint with Sex as expected. I don't see any reason to go from Sex to Patient. And isn't a Patient also a Person?
Did you populate the data? Have you tried executing a query in SSMS or reviewing the data?
If you do LazyLoading and not Include , then you should leave the connection open and floating , so in the View that want to access the properties can go to the database.
Member
35 Points
270 Posts
Model navigation property returns null value in my asp.net core project.
May 05, 2020 06:11 PM|hamed_1983|LINK
Hi
I have some entities like Patients with some relations to another entities. My problem is that, in Details view, the navigation properties (which points to other tables) does not display anything! apparently it returns null.
after a lot of search, some people says i should change some configation options in my dbContext (see this link), but i could not find Configuration property!
some people says Lazy loading is not supported by ef core, i should include it manually in my linq query (see this link), but also i could not find Include in my dbContext!!
Here is my Patients class :
And here is my another table (Sex) :
And here is my action method :
And here is my Details view code :
Can anybody help me where is my problem & how to solve it?
Thanks in advance
All-Star
120166 Points
27994 Posts
Moderator
MVP
Re: Model navigation property returns null value in my asp.net core project.
May 05, 2020 06:37 PM|ignatandrei|LINK
Member
35 Points
270 Posts
Re: Model navigation property returns null value in my asp.net core project.
May 05, 2020 06:41 PM|hamed_1983|LINK
Thanks for reply
But as i told in my first post, i don't have Include method in my dbContext!
All-Star
53121 Points
23675 Posts
Re: Model navigation property returns null value in my asp.net core project.
May 05, 2020 08:16 PM|mgebhard|LINK
I assume Sex can only be Male or Female? if so, Sex should be a lookup table or an enum.
Reference documentation
https://www.learnentityframeworkcore.com/configuration/one-to-many-relationship-configuration
https://codewithshadman.com/entity-framework-enum-code-first/
Member
35 Points
270 Posts
Re: Model navigation property returns null value in my asp.net core project.
May 05, 2020 09:36 PM|hamed_1983|LINK
Hi mgebhard
But i've misunderstood your code. I think your sample code does not solve my problem.
The 'Sex' entity is a table in database which has a relationship with 'Patients' table via 'SexID' column.
Here is my db context OnModelCreating method :
Do am i missing some package or references ?
Thanks in advance
All-Star
53121 Points
23675 Posts
Re: Model navigation property returns null value in my asp.net core project.
May 05, 2020 10:05 PM|mgebhard|LINK
Your code does not work.
My code works and Patient has a foreign key constraint with Sex as expected. I don't see any reason to go from Sex to Patient. And isn't a Patient also a Person?
Did you populate the data? Have you tried executing a query in SSMS or reviewing the data?
Member
35 Points
270 Posts
Re: Model navigation property returns null value in my asp.net core project.
May 06, 2020 08:39 AM|hamed_1983|LINK
Thanks for reply
I've solved my problem.
To enable Lazy Loading in ef core, we must add this package to the solution :
Then, in ConfigureServices method (in StartUp.cs) we can enable lazy loading proxies as follow :
Reference : https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.proxiesextensions.uselazyloadingproxies?view=efcore-3.1
Best regards.
All-Star
120166 Points
27994 Posts
Moderator
MVP
Re: Model navigation property returns null value in my asp.net core project.
May 06, 2020 10:57 AM|ignatandrei|LINK
If you do LazyLoading and not Include , then you should leave the connection open and floating , so in the View that want to access the properties can go to the database.
All-Star
53121 Points
23675 Posts
Re: Model navigation property returns null value in my asp.net core project.
May 06, 2020 11:17 AM|mgebhard|LINK
I recommend using Include()
Member
35 Points
270 Posts
Re: Model navigation property returns null value in my asp.net core project.
May 06, 2020 11:23 AM|hamed_1983|LINK
The Include method can access after adding this package :
Best regards