C# ForeignKey赢得';t work | ASP.NET MVC 5应用程序
我正在尝试将发布到数据库的消息与发送它的相应用户ID连接起来 但它不起作用,并返回错误:无法将值NULL插入“UserId”列,表“aspnet-idelabs10-20160411101634.dbo.Ides”;列不允许空值。插入失败。 声明已终止 这很符合逻辑,因为它不能为null 这是我的代码:C# ForeignKey赢得';t work | ASP.NET MVC 5应用程序,c#,mysql,asp.net-mvc,foreign-keys,C#,Mysql,Asp.net Mvc,Foreign Keys,我正在尝试将发布到数据库的消息与发送它的相应用户ID连接起来 但它不起作用,并返回错误:无法将值NULL插入“UserId”列,表“aspnet-idelabs10-20160411101634.dbo.Ides”;列不允许空值。插入失败。 声明已终止 这很符合逻辑,因为它不能为null 这是我的代码: namespace idelabs10.Migrations { using System; using System.Data.Entity.Migrations; public parti
namespace idelabs10.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class second : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Ides",
c => new
{
IdeId = c.Int(nullable: false, identity: true),
Emne = c.String(),
Forslag = c.String(),
UserId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.IdeId)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
CreateTable(
"dbo.AspNetUserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.String(nullable: false, maxLength: 128),
ClaimType = c.String(),
ClaimValue = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.AspNetUserLogins",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128),
ProviderKey = c.String(nullable: false, maxLength: 128),
UserId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
}
表“Ides”无法按我所希望的方式运行,因为它似乎无法从aspnetuser请求userid
有什么想法吗?
我对这个很陌生:)
名为Forslag2的控制器的代码
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 idelabs10.Models;
namespace idelabs10.Controllers
{
public class Forslag2Controller : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Forslag2
public ActionResult Index()
{
return View(db.Ides.ToList());
}
// GET: Forslag2/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ide ide = db.Ides.Find(id);
if (ide == null)
{
return HttpNotFound();
}
return View(ide);
}
// GET: Forslag2/Create
public ActionResult Create()
{
return View();
}
// POST: Forslag2/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 ActionResult Create([Bind(Include = "IdeId,Emne,Forslag,UserId")] Ide ide)
{
if (ModelState.IsValid)
{
db.Ides.Add(ide);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(ide);
}
// GET: Forslag2/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ide ide = db.Ides.Find(id);
if (ide == null)
{
return HttpNotFound();
}
return View(ide);
}
// POST: Forslag2/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 ActionResult Edit([Bind(Include = "IdeId,Emne,Forslag,UserId")] Ide ide)
{
if (ModelState.IsValid)
{
db.Entry(ide).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(ide);
}
// GET: Forslag2/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ide ide = db.Ides.Find(id);
if (ide == null)
{
return HttpNotFound();
}
return View(ide);
}
// POST: Forslag2/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Ide ide = db.Ides.Find(id);
db.Ides.Remove(ide);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
首先,您需要激活该操作的授权,以便只允许登录帖子。如果您使用的是标准的MVC应用程序,那么它非常简单,只需向该方法添加“Authorize”属性即可。接下来,从环境中提取UserId并将其用于插入,请参见下面的示例(为了避免混淆,我从传入成员中删除了UserId):
首先,您需要激活该操作的授权,以便只允许登录帖子。如果您使用的是标准的MVC应用程序,那么它非常简单,只需向该方法添加“Authorize”属性即可。接下来,从环境中提取UserId并将其用于插入,请参见下面的示例(为了避免混淆,我从传入成员中删除了UserId):
您从环境中获取用户ID,请参见以下问题:如果您仍然存在问题,请共享控制器代码,我们从那里获取。我为控制器“forslag2”添加了代码对不起,我会尝试清理一下。您从环境中获取用户ID,请参见以下问题:如果您仍然存在问题,请分享你的控制器代码,我们从那里获取。我添加了控制器“forslag2”的代码对不起,我会尝试清理一下
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult Create([Bind(Include = "IdeId,Emne,Forslag")] Ide ide)
{
if (ModelState.IsValid)
{
ide.UserId = User.Identity.GetUserId()
db.Ides.Add(ide);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(ide);
}