I got a problem on Foreign Keys.
What I want is that for every Video Model there is a Profile Model link to it
An example of that would look like this:
public class VideoModels
{
[Required]
public int Id { get; set; }
public ProfileModels Profile { get; set; }
}
While in the ProfileModels
public class ProfileModels
{
[Required]
public int Id { get; set; }
public string Title { get; set; }
}
Which technically should not have any connection with VideoModels for the VideoModels is dependent with ProfileModels and there can be 0 to many relationship.
So after that I tested it, my VideoModels has a Profile_Id parameter which is a foreign key from ProfileModels
But after creating an object and retrieving it, it returns NULL but when I check the database there is an existing Foreign Key Id.
Please help, stuck here for hours now
Profile would be null here due to LazyLoading. This allows us to have an object with many different objects that are only loaded when needed rather than loaded when a parent object is loaded. This is especially helpful if you have something like a Comments property as part of the user. Without LazyLoading the comments collection would be loaded whenever you wanted to grab just the VideoModels.ID. A downside to LazyLoading is that each navigation performed requires a separate query to the data source.
To allow for LazyLoading you need to mark related object properties as virtual.
In addition you must supply a navigation property in your context:
public class Database : DbContext {
public DbSet<VideoModels> People { get; set; }
public DbSet<ProfileModels> PersonDetails { get; set; }
}
Or
If you want to use your current class modeling, you can achieve this by disabling LazyLoading.
this.Configuration.LazyLoadingEnabled = false;
Nevermind, I just forgot to make the foreign key propery as virtual.
public class VideoModels
{
[Required]
public int Id { get; set; }
public virtual ProfileModels Profile { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With