gpt4 book ai didi

c# - 外键不起作用 | ASP .NET MVC 5 应用程序

转载 作者:行者123 更新时间:2023-11-29 07:19:12 26 4
gpt4 key购买 nike

我正在尝试将发布到数据库的消息与发送该消息的相应 userId 连接起来。

但它不起作用,并返回错误:无法将 NULL 值插入表“aspnet-idelabs10-20160411101634.dbo.Ides”的列“UserId”中;列不允许为空。插入失败。该声明已终止。

这是非常符合逻辑的,因为它不能为空。

这是我的代码:

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 请求用户 ID。

有什么想法吗?顺便说一句,我对这个还很陌生:)

名为 Forslag2 的 Controller 的代码

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 以避免混淆):

[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);
}

关于c# - 外键不起作用 | ASP .NET MVC 5 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36764527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com