0

是否可以将列(存储为逗号分隔)转换为在 edmx 设计器中显示为另一个实体?

表结构

student
  id
  name
  subjects << this is the comma delimited column (1,2,3,10)

subject
  id
  name

EDMX 设计器结构将是

student 
  id
  name

student_subject
  studentId
  subjectid

subject
  id
  name   

如果是这样,有人可以指出我如何在 EF 设计器上做到这一点。

注意:我无法更改表结构,我首先使用数据库

谢谢

4

1 回答 1

0

There is no way to do this. Worse: in SQL it might be possible to fetch the subjects with an IN clause, but with linq you haven't even got that option. I'm afraid what's left is a painstaking linq-to-objects operation where you first grab the Student from the database, then parse the subjects in an int array, and finally grab the Subjects with a Contains query:

var subjectIds = student.Subjects
    .Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
    .Select (x => int.Parse(x));

var subjects = context.Subjects.Where(s => subjectIds.Contains(s.Id));

(And don't forget to send the architect as student to a course with subject 'Data base design')

于 2012-11-15T21:16:11.217 回答