【发布时间】:2022-04-16 11:56:37
【问题描述】:
我使用 Entity Framework 在 ASP.NET Core razor pages CRUD 中构建了一个 Book 应用。但是,当我运行该应用程序并在 https://localhost:44370/ 上查看它时,外键字词显示为空白。这是我的代码:
模型作者
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BookStore.Models
{
public class Author
{
public int ID { get; set; }
[StringLength(50)]
[Display(Name = "Country Name")]
public string CountryName { get; set; }
[StringLength(50)]
[Display(Name = "Author Name")]
public string AuthorName { get; set; }
[ForeignKey("AuthorID")]
public ICollection<Book> Books { get; set; }
[ForeignKey("AuthorID")]
public ICollection<Tour> Tours { get; set; }
}
}
模型书类
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BookStore.Models
{
public class Book
{
[Key]
public int BookID { get; set; }
[StringLength(50)]
[Column("BookTitle")]
[Display(Name = "Book Title")]
public string BookTitle { get; set; }
[StringLength(50)]
[Column("BookPublisher")]
[Display(Name = "Book Publisher")]
public string BookPublisher { get; set; }
[ForeignKey("AuthorID")]
public Author Author { get; set; }
public int AuthorID { get; set; }
public ICollection<BookReview> BookReviews { get; set; }
}
}
模特巡回课
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BookStore.Models
{
public class Tour
{
[Key]
public int TourID { get; set; }
[StringLength(50)]
[Column("TourName")]
[Display(Name = "Tour Name")]
public string TourName { get; set; }
[StringLength(50)]
[Column("TourPlace")]
[Display(Name = "Tour Place")]
public string TourPlace { get; set; }
[ForeignKey("AuthorID")]
public Author Author { get; set; }
public int AuthorID { get; set; }
public ICollection<BookRating> BookRating { get; set; }
}
}
最初,当我为 Book 文件夹和 Tour 文件夹搭建页面时,生成的 Create.cshtml.cs 和 Edit.cshtml.cs 页面将外键显示为“CountryName”,因此我将其修改为“AuthorName” "
页数:书:Create.cshtml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using BookStore.Models;
namespace BookStore.Pages.Books
{
public class CreateModel : PageModel
{
private readonly BookStore.Models.BookWorkContext _context;
public CreateModel(BookStore.Models.BookWorkContext context)
{
_context = context;
}
public IActionResult OnGet()
{
ViewData["AuthorID"] = new SelectList(_context.Author, "ID", "AuthorName");
return Page();
}
[BindProperty]
public Book Book { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Book.Add(Book);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}
数据:'DbInitializer.cs'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using BookStore.Models;
namespace BookStore.Data
{
public class DbInitializer
{
public static void Initialize(BookWorkContext context)
{
context.Database.EnsureCreated();
// Look for any Authors.
if (context.Author.Any())
{
return; // DB has been seeded
}
var Authors = new Author[]
{
new Author {CountryName="United States of America",AuthorName="John Coltrane"},
new Author {CountryName= "United States of America",AuthorName= "Jim Meyers"}
};
foreach (Author a in Authors)
{
context.Author.Add(a);
}
context.SaveChanges();
var Tours = new Tour[]
{
new Tour {AuthorID = Authors.Single(a => a.AuthorName == "John Coltrane").ID,TourName = "The Prairie Tour",TourPlace = "Town Center"},
new Tour {AuthorID = Authors.Single(a => a.AuthorName == "Jim Meyers").ID,TourName = "Santa Barbara Tours",TourPlace = "Long Circular Mall"},
};
foreach (Tour t in Tours)
{
context.Tour.Add(t);
}
context.SaveChanges();
var Books = new Book[]
{
new Book {AuthorID = Authors.Single(a => a.AuthorName == "Tim Hendricks").ID, BookTitle = "Three Stooges",BookPublisher = "Penguin"},
new Book {AuthorID = Authors.Single(a => a.AuthorName == "Andy Ford").ID, BookTitle = "Tom Sawyer",BookPublisher = "Collins"},
};
foreach (Book b in Books)
{
context.Book.Add(b);
}
context.SaveChanges();
}
}
}
在运行应用程序时,如果我尝试创建一个新条目,我会在下拉字段中看到 AuthorName,但是一旦我保存了新条目,外键列是空白的,而其他列则填充了数据。
我的问题是,一旦我运行应用程序,如何让外键词“AuthorName”出现在应用程序中?
【问题讨论】:
-
你也可以发布你的剃须刀页面吗?
-
@Patrick Mcvay:我发布了 Book razor 页面。谢谢。
-
为什么您有相同的“AuthorId”,如用于书籍收藏和旅游收藏的伪造密钥。为什么你有伪造的钥匙来收集?
-
您是否在 Linquery 中包含 AUTHOR 表? ex _context.Books.Include(x=>x.Author).ToList();
-
另一件事是,在 c# winforms 中,您需要在构造函数中初始化列表。我不知道 .net 核心是否需要它,但你也错过了
标签: c# asp.net-core entity-framework-core razor-pages