我得到的结果是:
但是正如您所看到的,我的某些类返回空结果。这是我的代码:
MyDBContext:
public partial class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
this.Configuration.ProxyCreationEnabled = false;
//this.Configuration.LazyLoadingEnabled = false;
}
public virtual DbSet<Announcement> Announcement { get; set; }
public virtual DbSet<Class> Class { get; set; }
public virtual DbSet<Discontinuity> Discontinuity { get; set; }
public virtual DbSet<Grade> Grade { get; set; }
public virtual DbSet<Group> Group { get; set; }
public virtual DbSet<Lesson> Lesson { get; set; }
public virtual DbSet<Management> Management { get; set; }
public virtual DbSet<Payment> Payment { get; set; }
public virtual DbSet<Questions> Questions { get; set; }
public virtual DbSet<Student> Student { get; set; }
public virtual DbSet<Teacher> Teacher { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Class>()
.HasMany(e => e.Student)
.WithOptional(e => e.Class)
.HasForeignKey(e => e.Class_Id);
modelBuilder.Entity<Group>()
.HasMany(e => e.Student)
.WithOptional(e => e.Group)
.HasForeignKey(e => e.Group_Id);
modelBuilder.Entity<Lesson>()
.HasMany(e => e.Teacher)
.WithRequired(e => e.Lesson)
.HasForeignKey(e => e.Lesson_Id)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Student>()
.HasMany(e => e.Discontinuity)
.WithOptional(e => e.Student)
.HasForeignKey(e => e.Student_Id);
modelBuilder.Entity<Student>()
.HasMany(e => e.Grade)
.WithOptional(e => e.Student)
.HasForeignKey(e => e.Student_Id);
modelBuilder.Entity<Student>()
.HasMany(e => e.Payment)
.WithOptional(e => e.Student)
.HasForeignKey(e => e.Student_Id);
}
}
我的控制器类别:
public class StudentsController : ApiController
{
private MyContext db = new MyContext();
// GET: api/Students
public IQueryable<Student> GetStudent()
{
return db.Student;
}
// GET: api/Students/5
[ResponseType(typeof(Student))]
public IHttpActionResult GetStudent(int id)
{
Student student = db.Student.Find(id);
if (student == null)
{
return NotFound();
}
return Ok(student);
}
// PUT: api/Students/5
[ResponseType(typeof(void))]
public IHttpActionResult PutStudent(int id, Student student)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != student.Id)
{
return BadRequest();
}
db.Entry(student).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!StudentExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Students
[ResponseType(typeof(Student))]
public IHttpActionResult PostStudent(Student student)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Student.Add(student);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = student.Id }, student);
}
// DELETE: api/Students/5
[ResponseType(typeof(Student))]
public IHttpActionResult DeleteStudent(int id)
{
Student student = db.Student.Find(id);
if (student == null)
{
return NotFound();
}
db.Student.Remove(student);
db.SaveChanges();
return Ok(student);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool StudentExists(int id)
{
return db.Student.Count(e => e.Id == id) > 0;
}
}
我该如何补习国外课程?我很好奇,有什么方法可以在结果开头输入类名。例如。如果我在学生中,则应以:“ Student”:[{“ Id”:18}]开始。我已经有了ModelBuilder。不,我真的不知道我在哪里做错了吗?
答案 0 :(得分:1)
您需要.Include
那些附加属性,这些属性将指示EF去为您检索数据。
为此,您需要将语法稍作更改:
var student = db.Student
.Include(i => i.Discontinuity)
.Include(i => i.Payment)
.SingleOrDefault(s => s.Id == id);