![](/img/trans.png)
[英]Handeling my Many-to-Many relation api requests in asp.net core3.1 webapi
[英]relation one to many in asp.net core webapi
我是asp.net的新手,這是我的第一個項目。 我有兩個學生餐桌和筆記。 學生有很多筆記,所以我想顯示學生列表及其筆記列表,但是郵遞員中出現此錯誤。解析JSON數據時出錯JSON輸入意外結束。 並在控制台中檢測到類型為“ firstExp.Models.Student”的屬性“ student”的自引用循環。 路徑“ [0] .notes [0]”。
這是學生模型
public class Student
{
public int StudentId { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string City { get; set; }
public string State { get; set; }
public ICollection<Notes> Notes { get; set; }
= new List<Notes>();
}
這是筆記模型
public class Notes {
public int NotesId { get; set; }
[Required]
public string NoteValue { get; set; }
[Required]
public string Subject { get; set; }
[ForeignKey ("StudentId")]
public Student Student { get; set; }
public int StudentId { get; set; }
}
這是學生負責人
namespace firstExp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : Controller
{
private StudentContext _studentContext;
public StudentController(StudentContext context)
{
_studentContext = context;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<Student>> Get()
{
return _studentContext.Students.OrderBy(c => c.FirstName).Include(c => c.Notes).ToList();
}
}
}
this is OnModelCreating method
protected override void OnModelCreating (ModelBuilder builder) {
base.OnModelCreating (builder);
builder.Entity<Student> ().ToTable ("Students");
builder.Entity<Student> ().HasKey (p => p.StudentId);
builder.Entity<Student> ().Property (p => p.StudentId).IsRequired ().ValueGeneratedOnAdd ();
builder.Entity<Student> ().Property (p => p.FirstName).IsRequired ().HasMaxLength (30);
builder.Entity<Student> ().HasMany (p => p.Notes).WithOne (p => p.Student).HasForeignKey (p => p.StudentId);
builder.Entity<Notes> ().ToTable ("Notes");
builder.Entity<Notes> ().HasKey (p => p.NotesId);
builder.Entity<Notes>().Property(p => p.NotesId).IsRequired().ValueGeneratedOnAdd();
builder.Entity<Notes>().Property(p => p.NoteValue).IsRequired().HasMaxLength(50);
builder.Entity<Notes>().Property(p => p.Subject).IsRequired();
}
}
我做錯了什么? 謝謝。
您的Notes課程:
public class Notes {
public int NotesId { get; set; }
[Required]
public string NoteValue { get; set; }
[Required]
public string Subject { get; set; }
public Student Student { get; set; }
[ForeignKey ("Student")]
public int StudentId { get; set; }
}
在您的SchollContext
類中,重寫DbContext.OnModelCreating(),然后在其中定義所需的關系。 檢查文檔 ,看看如何做到這一點。 在您的SchoolContext類中,聲明DbSet:
public DbSet<Student> Students { get; set; }
public DbSet<Notes> Notes { get; set; }
您的OnModelCreating()應該看起來像這樣:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Notes>().HasOne<Student>(n => n.Student)
.WithMany(s => s.Notes)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Student>().ToTable("Students");
modelBuilder.Entity<Notes>().ToTable("Notes");
base.OnModelCreating(modelBuilder);
}
現在,刪除所有遷移並刪除表或數據庫,然后再次應用遷移。 希望有幫助!
您可以將Json.NET
配置為忽略它在對象圖中找到的循環。 這是在Startup.cs的ConfigureServices
方法中完成的。
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
...
}
請參閱相關數據和序列化
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.