EF Lazy Load Collection - entity-framework-6

I am trying to get my head around EF.
I have this entity
public class Project : IEntity
{
public int Id { get; set; }
public String Title { get; set; }
public String Description { get; set; }
public int ProjectTypeId { get; set; }
public string UserId { get; set; }
public bool Active { get; set; }
public int ProjectStatusId { get; set; }
public virtual ICollection<WorkItem> WorkItems { get; set; }
public ProjectType ProjectType { get; set; }
public ProjectStatus ProjectStatus { get; set; }
}
I want the collection "WorkItems" to be lazy loaded but project type and project status should be loaded up front.
My question is how do I set this up and how can I see that It's lazy loading or loading it up front without installing sql profiler.
N.B I have set lazy loading false for serialization reasons
Thanks

Assuming you have context that looks something like:
public DbSet<Project> Projects { get; set; }
You could then call:
MyContext db = new MyContext();
db.Projects.Include(x => x.ProjectType).Include(y => y.ProjectStatus).Where...
This should eager load ProjectType and ProjectStatus but not WorkItems. You can confirm what EF is doing behind the scene by taking log output to your debug window:
db.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
Do not forget to put using System.Data.Entity; otherwise you will not be able to use lambda expressions in your include.
If you want to make sure that users of your data layer have ProjectType and ProjectStatus always loaded, you may consider implementing your own repository instead of DbSet.
Hope this helps.

Related

Entity Framework 6 and Chained Relationships

Having some issues with relationships within EntityFramework 6.
I know that DataAnnotations or FluentApi can be used and I'm okay with using either.
Here's an example of relationship I'd like to accomplish:
Student has one ImmunizationRecord
ImmunizationRecord has Multiple ShotRecords
This seems like it would be fairly straight forward, however it doesn't seem to be working as expected.
Here's example code (Updated from actual code)
public class Student : Entity
{
[Key]
public int Id { get; set; }
public String Name { get; set; }
//...
[ForeignKey(nameof(Id))]
public virtual ImmunizationRecord ImmunizationRecord { get; set; }
}
public class ImmunizationRecord : Entity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Key]
[ForeignKey(nameof(Student))]
public int StudentId { get; set; }
public String Name { get; set; }
public DateTime LastUpdated { get; set; }
public virtual Student Student { get; set; }
public virtual ICollection<ShotRecord> ShotRecords { get; set; }
}
public class ShotRecord: Entity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
// Want this to point back to ImmunizationRecord
public int ImmunizationRecordId { get; set; }
public String Name { get; set; }
public String Description { get; set; }
public DateTime DateOfShot { get; set; }
//...
[ForeignKey("ImmunizationRecordId")]
public virtual ImmunizationRecord ImmunizationRecord { get; set; }
}
Example fluentapi might be something like this:
modelBuilder.Entity<Student>().HasOptional(c => c.ImmunizationRecord).WithRequired(m => m.Student);
modelBuilder.Entity<Student>().HasOptional(c => c.ImmunizationRecord).WithRequired(sc => sc.Student);
modelBuilder.Entity<ImmunizationRecord>().HasMany(sc => sc.ShotRecords).WithRequired(sr => sr.ImmunizationRecord);
The Result
I suspect that I'm just missing a small piece of what needs to be done, or missing the proper way to configure these entities with a similar relationship.
With the code above and class structure, I can create a Student, and Create a ImmunizationRecord, and ShotRecords without issue.
The issue occurs when I try to retrieve the ShotRecords from The ImmunizationRecord, EntityFramework will resolve on the key on the Student.Id instead of using the key of on the ImmunizationRecord.Id.
I can go into the database and change the rows for ShotRecords and update the ImmunizationRecordId to the StudentId and they'll resolve properly. But as stated before, I want them to use the key of the ImmunizationRecord, and not the student.
Any advice and guidance would be greatly appreciated!
Thank you in advance!
(Updated to a different example to make more sense)

Viewmodel set up Aspt.net MVC 6

I'm having trouble understanding how to implement a ViewModel in Asp.net MVC, I have the following tables:
Form
ID, Data
Report
ID, FormID, Owner, Category, Status, SubmissionDate
ReportValues
ID, ReportID, Title, Value
I'm looking for a way to display and edit Report and ReportValues in the one ViewModel where ReportValues.ReportID = Report.ID
ReportValues will have multiple entries that relate to a Report.
I have had a look at similiar questions on here and tried following a tutorial ( http://techfunda.com/howto/262/list-data-using-viewmodel ) and coming up empty handed.
If you need any more information let me know and thanks in advance for any replies!
Your View Model is nothing more than a class. You can solve this many ways, but here's an example.
Create your 3 classes like you normally would.
public class Form
{
public int Id { get; set; }
public string Data { get; set; }
}
public class ReportValues
{
public int Id { get; set; }
public int ReportId { get; set; }
public string Title { get; set; }
public string Value { get; set; }
}
public class Report
{
public int Id { get; set; }
public int FormId { get; set; }
public string Owner { get; set; }
public string Category { get; set; }
public string Status { get; set; }
public DateTime SubmissionDate { get; set; }
}
Then, create your ViewModel class to include the three above classes like this.
public class ReportViewModel
{
public Form Form { get; set; }
public ReportValues ReportValues { get; set; }
public Report Report { get; set; }
}
In your view you can access your three classes and their properties as you would in your controller. Model.Form.Id
Depending on your data types, ReportValues will likely be a property of Report, but that's entirely up to your data structure. You will need to populate the classes using whatever method you want (Entity Framework, ADO, etc.) before you can pass them to your view and use them.

How do you match a MVC model class to existing database table and schema

I am using Visual Studio 2012, MVC 4, and Razor (CSHTML). I created Person.Person table in a test database. I wanted to have my model PersonModels.cs use this table so I created the following 2 classes.
public class Person
{
[Key]
public int BusinessEntityID { get; set; }
public string PersonType { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Suffix { get; set; }
public int EmailPromotion { get; set; }
[XmlAttribute(DataType = "string")]
public string AdditionalContactInfo { get; set; }
[XmlAttribute(DataType = "string")]
public string Demographics { get; set; }
public string rowguid { get; set; }
public DateTime ModifiedDate { get; set; }
}
public class PersonDbContext : DbContext
{
public DbSet<Person> person { get; set; }
}
I thought that it would pick the Person.Person table since the class name was Person even though I had not included the schema. However, upon running the application and doing an insert I checked the Person.Person table but the row wasn't there. It created dbo.People table and inserted the row there! I double checked because I thought I might be drunk but I did not write People anywhere!
I read about reverse poco but I'd like to understand how this works more than making it work.
Edit: It worked! Please find the code I used attached:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace client_site.Models
{
[Table("Person", Schema = "Person")]
public class TestModel
{
[Key]
public int ID { get; set; }
}
public class DefaultConnectionX : DbContext
{
public DbSet<TestModel> test { get; set; }
}
}
dbo is the default database schema. If you want to create the table in the people schema you have to add an attribute
[Table("Person", Schema = "Person")]

create new controller - error running selected code generator

I am using Visual Studio Express 2013 for Web (specifically version 12.0.21005.1 REL). This is my first project using VS2013, I've been using VS2012 up until this point.
I am attempting to create a new controller in my asp.net MVC application. I am using Entity Framework 5 with code first (.NET 4.5). I want Visual Studio to create the template for me (you know, a controller with views to read/write/delete etc, rather than write the code myself from scratch).
However, every time I try to create the controller I get the following error message:
Is there some sort of bug in VS 2013? I can't figure out what this means, and restarting VS2013 does not help.
Here are the gory details.... actually it is all very simple since this is a new project with very little code written so far.
My model:
namespace ProfessionalSite.Models
{
public class EntityModels
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int ID { get; set; }
public string EnrollmentName { get; set; }
public string Credits { get; set; }
}
// Create the class that inherits from DbContext
// The name of this class is also used as the connection string in web.config
public class EFDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
}
}
And in my web.config file I have the following
<add name="EFDbContext"
connectionString="Data Source=JONSNA\SQLEXP2012WT;Initial Catalog=ProfessionalSiteDb; Integrated Security=True; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient"/>
within the tags.
Now time to create a controller. I right click on Controllers in the Solution Explorer, and choose to Add a new Controller.
And then
And when I click Add I get
I cant figure out how to get rid of this error. I guess as a workaround I can just type the code myself, but I'd like to know if this is a bug or something I have done wrong. In VS2012 this just worked...
I'd appreciate any help or pointers. Thanks.
You don't need the EntityModels class, See below:
namespace ProfessionalSite.Models
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int ID { get; set; }
public string EnrollmentName { get; set; }
public string Credits { get; set; }
}
// Create the class that inherits from DbContext
// The name of this class is also used as the connection string in web.config
public class EFDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
}
Then when you create a controller, just select the Student or Enrollment for the Model class.

Alternative for enum - when seeding a database

I am right now in the design phase of a new app and am doing some research. I came across the CodeFirst approach of EF 4.3 and really liked it.
However there is one design goal, I am not sure how to achieve.
Let say I have a task class:
public class TaskModel
{
public int ID { get; set; }
public string Name { get; set; }
public TaskType Task { get; set; }
}
public enum TaskType
{
Sales = 0,
Marketing = 1,
CustomerService = 2
}
I know that enums are currently not supported in EF 4.3. Hence this code would not even generate a proper database model. However I don't even need enums. Since what if the user would like to add a new TaskType at runtime?
Hence I think its best to have the TaskType as a class, which would become a table in itself and the user could add more entries. But how do I make them map together?
In such case it is common one-to-many relation:
public class TaskModel
{
public int ID { get; set; }
public string Name { get; set; }
public virtual TaskType Task { get; set; }
}
public class TaskType
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<TaskModel> TaskModels { get; set; }
}

Resources