EF中一对多的关系中,用单字段保存ID拼接字符串

model.SubjectTerms = model.SelectedSubjectTerm.Trim(',').Split(',').ToList().Select(x => new SubjectTerm { SubjectTermId = int.Parse(x) }).ToList();

其中 SelectedSubjectTerm 是保存ID 用逗号隔开的字符串

使用split分割 然后再封装成SubjectTerm 实体

if (null != model.SubjectTerms)
                model.SelectedSubjectTerm = "," + string.Join(",", model.SubjectTerms.Select(x => x.SubjectTermId)) + ",";

反过来合并id

你可能感兴趣的:(EF中一对多的关系中,用单字段保存ID拼接字符串)