我正在创建一个带注册和身份验证的应用。 我正在使用这篇文章。 https://azure.microsoft.com/en-gb/documentation/articles/web-sites-dotnet-deploy-aspnet-mvc-app-membership-oauth-sql-database/
在启用迁移之前,如文章中那样创建控制器后出现错误。当然,我无法启用迁移会导致很多错误。 我该怎么办?
我搜索了答案,但主要是'字符串'到' int'或类似的东西。
完整错误: CS0029 C#无法隐式转换类型' PingGod.Models.ApplicationUser'到' PingGod.Models.User'
我也得到了一些其他错误,但可能只是相反的含义: CS1503 C#参数1:无法转换为' PingGod.Models.User'到' PingGod.Models.ApplicationUser'
提前感谢您的帮助!
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using PingGod.Models;
namespace PingGod.Controllers
{
public class UsersController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Users
public ActionResult Index()
{
return View(db.Users.ToList());
}
// GET: Users/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Users users = db.Users.Find(id); //Here I get error (CS0029)
if (users == null)
{
return HttpNotFound();
}
return View(users);
}
// GET: Users/Create
public ActionResult Create()
{
return View();
}
// POST: Users/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "UserId,Name,Address,City,State,Zip,Email,url")] Users users)
{
if (ModelState.IsValid)
{
db.Users.Add(users); //Here an error also (CS1503)
db.SaveChanges();
return RedirectToAction("Index");
}
return View(users);
}
// GET: Users/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Users users = db.Users.Find(id); //Here error (CS0029)
if (users == null)
{
return HttpNotFound();
}
return View(users);
}
// POST: Users/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "UserId,Name,Address,City,State,Zip,Email,url")] Users users)
{
if (ModelState.IsValid)
{
db.Entry(users).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(users);
}
// GET: Users/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Users users = db.Users.Find(id); //Here eror (CS0029)
if (users == null)
{
return HttpNotFound();
}
return View(users);
}
// POST: Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Users users = db.Users.Find(id); // Here also an error (CS0029)
db.Users.Remove(users); //Here other error (CS1503)
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
答案 0 :(得分:1)
两个用户类都不同,这就是您收到此错误的原因
使用
PingGod.Models.Users users = db.Users.Find(id)
也许这有助于你