上下文 int entity/net core 的问题
Troubles with context int entity/ net core
为我的模型创建控制器后遇到问题。这是主要型号:
public class Event
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[DataType(DataType.Date)]
[Required]
public DateTime Date { get; set; }
public Subcategory Subcategory { get; set; }
public int SubcategoryID { get; set; }
public Place Place { get; set; }
public int PlaceID { get; set; }
public Organisator Organisator { get; set; }
public int OrganisatorID { get; set; }
public ICollection<User_link> User_link { get; set; }
}
这里其他模型,如子类别、地点和组织者具有一对多(事件)绑定。
这里是地方例如:
public class Place
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string Call_number { get; set; }
public string Email { get; set; }
public string Vk { get; set; }
public string Facebook { get; set; }
public ICollection<Event> Event { get; set; }
}
所以我收集了每个地方的事件。还有事件控制器
public class EventsController : Controller
{
private readonly ApplicationDbContext _context;
public EventsController(ApplicationDbContext context)
{
_context = context;
}
// GET: Events
public async Task<IActionResult> Index()
{
var applicationDbContext = _context.Event.Include(@ => @.Organisator).Include(@ => @.Place).Include(@ => @.Subcategory);
return View(await applicationDbContext.ToListAsync());
}
// GET: Events/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var @event = await _context.Event
.Include(@ => @.Organisator)
.Include(@ => @.Place)
.Include(@ => @.Subcategory)
.SingleOrDefaultAsync(m => m.ID == id);
if (@event == null)
{
return NotFound();
}
return View(@event);
}
// GET: Events/Create
public IActionResult Create()
{
ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title");
ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address");
ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title");
return View();
}
// POST: Events/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Title,Description,Date,SubcategoryID,PlaceID,OrganisatorID")] Event @event)
{
if (ModelState.IsValid)
{
_context.Add(@event);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
return View(@event);
}
// GET: Events/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var @event = await _context.Event.SingleOrDefaultAsync(m => m.ID == id);
if (@event == null)
{
return NotFound();
}
ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
return View(@event);
}
// POST: Events/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Title,Description,Date,SubcategoryID,PlaceID,OrganisatorID")] Event @event)
{
if (id != @event.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(@event);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EventExists(@event.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
return View(@event);
}
// GET: Events/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var @event = await _context.Event
.Include(@ => @.Organisator)
.Include(@ => @.Place)
.Include(@ => @.Subcategory)
.SingleOrDefaultAsync(m => m.ID == id);
if (@event == null)
{
return NotFound();
}
return View(@event);
}
// POST: Events/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var @event = await _context.Event.SingleOrDefaultAsync(m => m.ID == id);
_context.Event.Remove(@event);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool EventExists(int id)
{
return _context.Event.Any(e => e.ID == id);
}
}
每个@=>@.Place 都会抛出 2 个错误:
CS0119 'Place' 是一种类型,在给定的上下文 EventsPortal 中无效
CS1646 逐字说明符后需要关键字、标识符或字符串:@
一切都在一个环境中
在 C# 中,@ 是逐字说明符,用于多行字符串表达式以忽略转义字符和换行符。
只需用一些有效的标识符替换它,即 lambda 表达式中的 'x'、'a',它应该可以工作。
这里是 Multiline String Literal in C# 使用 @ 的例子。
为我的模型创建控制器后遇到问题。这是主要型号:
public class Event
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[DataType(DataType.Date)]
[Required]
public DateTime Date { get; set; }
public Subcategory Subcategory { get; set; }
public int SubcategoryID { get; set; }
public Place Place { get; set; }
public int PlaceID { get; set; }
public Organisator Organisator { get; set; }
public int OrganisatorID { get; set; }
public ICollection<User_link> User_link { get; set; }
}
这里其他模型,如子类别、地点和组织者具有一对多(事件)绑定。
这里是地方例如:
public class Place
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string Call_number { get; set; }
public string Email { get; set; }
public string Vk { get; set; }
public string Facebook { get; set; }
public ICollection<Event> Event { get; set; }
}
所以我收集了每个地方的事件。还有事件控制器
public class EventsController : Controller
{
private readonly ApplicationDbContext _context;
public EventsController(ApplicationDbContext context)
{
_context = context;
}
// GET: Events
public async Task<IActionResult> Index()
{
var applicationDbContext = _context.Event.Include(@ => @.Organisator).Include(@ => @.Place).Include(@ => @.Subcategory);
return View(await applicationDbContext.ToListAsync());
}
// GET: Events/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var @event = await _context.Event
.Include(@ => @.Organisator)
.Include(@ => @.Place)
.Include(@ => @.Subcategory)
.SingleOrDefaultAsync(m => m.ID == id);
if (@event == null)
{
return NotFound();
}
return View(@event);
}
// GET: Events/Create
public IActionResult Create()
{
ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title");
ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address");
ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title");
return View();
}
// POST: Events/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Title,Description,Date,SubcategoryID,PlaceID,OrganisatorID")] Event @event)
{
if (ModelState.IsValid)
{
_context.Add(@event);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
return View(@event);
}
// GET: Events/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var @event = await _context.Event.SingleOrDefaultAsync(m => m.ID == id);
if (@event == null)
{
return NotFound();
}
ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
return View(@event);
}
// POST: Events/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Title,Description,Date,SubcategoryID,PlaceID,OrganisatorID")] Event @event)
{
if (id != @event.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(@event);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EventExists(@event.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["OrganisatorID"] = new SelectList(_context.Organisator, "ID", "Title", @event.OrganisatorID);
ViewData["PlaceID"] = new SelectList(_context.Place, "ID", "Address", @event.PlaceID);
ViewData["SubcategoryID"] = new SelectList(_context.Subcategory, "ID", "Title", @event.SubcategoryID);
return View(@event);
}
// GET: Events/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var @event = await _context.Event
.Include(@ => @.Organisator)
.Include(@ => @.Place)
.Include(@ => @.Subcategory)
.SingleOrDefaultAsync(m => m.ID == id);
if (@event == null)
{
return NotFound();
}
return View(@event);
}
// POST: Events/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var @event = await _context.Event.SingleOrDefaultAsync(m => m.ID == id);
_context.Event.Remove(@event);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool EventExists(int id)
{
return _context.Event.Any(e => e.ID == id);
}
}
每个@=>@.Place 都会抛出 2 个错误: CS0119 'Place' 是一种类型,在给定的上下文 EventsPortal 中无效 CS1646 逐字说明符后需要关键字、标识符或字符串:@
一切都在一个环境中
在 C# 中,@ 是逐字说明符,用于多行字符串表达式以忽略转义字符和换行符。
只需用一些有效的标识符替换它,即 lambda 表达式中的 'x'、'a',它应该可以工作。
这里是 Multiline String Literal in C# 使用 @ 的例子。