开发者

Load Selected Primitive Properties in Entity Framework

开发者 https://www.devze.com 2023-03-30 20:15 出处:网络
I have following structure in Oracle database: Course(CourseId, Name) ->Student(StudentId, Name, Comment, CourseId)

I have following structure in Oracle database:

Course(CourseId, Name)
->Student(StudentId, Name, Comment, CourseId)
->Subject(SubjectId, Name, SubjectComment, CourseId)

Student contains some of Primitive Properties (StudentId, Name, CourseId, Comment) and Navigation Property (Courses [Linked with DTO name Course on CourseId]).

Table structure is also same as Entity structure and currenly using Explic开发者_如何学Cit loading to extract the data from Oracle database, using LoadProperty and Load.

I need to load the Collection and Object with selected property, as Load Student with StudentId and Name (without Comment column).

LoadProperty(Student, s => s.Courses), load only CourseId (don't load Name primitive property in Course DTO). So, Student.Courses.First().CourseId will be a value and Name will be null as intentionally excluded from loading from database.

LoadProperty(Course, c => c.Subjects) load only SubjectId without Name property, even don't go to database to load.

Is there any way to Include/Exclude the Primitive types to load?


Not with load property and not with entities. You must create custom query and use projection to load only selected columns:

var student = from s in context.Students
              select new {
                      StudentId = s.StudentId,
                      ... // Other student's properties
                      CourseId = s.Course.Id,
                      SubjectIds = s.Courese.Subjects.Select(s => s.Ids)   
                  };
0

精彩评论

暂无评论...
验证码 换一张
取 消