SQL Server数据库C#中的空值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server数据库C#中的空值相关的知识,希望对你有一定的参考价值。
我正在使用ASP.NET Core,Visual Studio Code创建注册/登录Web应用。当我填写“注册”页面标签(用户名,密码,电子邮件,全名)并单击“保存”按钮时,它会将值存储在数据库中,但所有内容均为NULL。
我使用VS Code(dotnet ef migrations add InitialCreate
..... dotnet ef database update
)进行迁移创建了数据库。类型是nvarchar(max)
,但我认为不是问题的原因。
这是我的代码:
迁移:
using Microsoft.EntityFrameworkCore.Migrations;
namespace probagetrequest.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Account",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Username = table.Column<string>(nullable: true),
Password = table.Column<string>(nullable: true),
FullName = table.Column<string>(nullable: true),
Email = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Account", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Account");
}
}
}
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using probagetrequest.Models;
namespace probagetrequest.Migrations
{
[DbContext(typeof(DatabaseContext))]
[Migration("20200411075219_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("probagetrequest.Models.Account", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Email")
.HasColumnType("nvarchar(max)");
b.Property<string>("FullName")
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.HasColumnType("nvarchar(max)");
b.Property<string>("Username")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Account");
});
#pragma warning restore 612, 618
}
}
}
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using probagetrequest.Models;
namespace probagetrequest.Migrations
{
[DbContext(typeof(DatabaseContext))]
partial class DatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("probagetrequest.Models.Account", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Email")
.HasColumnType("nvarchar(max)");
b.Property<string>("FullName")
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.HasColumnType("nvarchar(max)");
b.Property<string>("Username")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Account");
});
#pragma warning restore 612, 618
}
}
}
这里是模型类:
namespace probagetrequest.Models
{
[Table("Account")]
public class Account
{
[Key]
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
}
}
DatabaseContext
:
using Microsoft.EntityFrameworkCore;
namespace probagetrequest.Models
{
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public DbSet<Account> Accounts { get; set; }
}
}
这里是SignUp.cshtml.cs
:
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc;
using probagetrequest.Models;
namespace probagetrequest.Pages
{
public class SignUpModel : PageModel
{
[BindProperty]
public Account account { get; set; }
private DatabaseContext db;
public SignUpModel(DatabaseContext _db)
{
db = _db;
}
public void OnGet()
{
account = new Account();
}
public IActionResult OnPost()
{
account.Password = BCrypt.Net.BCrypt.HashPassword(account.Password);
db.Accounts.Add(account);
db.SaveChanges();
return RedirectToPage("index");
}
}
}
SignUp.cshtml
:
@page
@model probagetrequest.Pages.SignUpModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SignUp</title>
</head>
<body>
<h3>Sign Up</h3>
<form method="post" asp-page="signup">
<table>
<tr>
<td>Username</td>
<td>
<input aps-for="@Model.account.Username" />
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" asp-for="@Model.account.Password" />
</td>
</tr>
<tr>
<td>Full Name</td>
<td>
<input aps-for="@Model.account.FullName" />
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="email" aps-for="@Model.account.Email" />
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" value="Save" />
<br />
<a asp-page="index">Sign In</a>
</td>
</tr>
</table>
</form>
</body>
</html>
答案
请注意您的拼写,应该是asp-for
而不是aps-for
。否则您的模型绑定将不起作用。
以上是关于SQL Server数据库C#中的空值的主要内容,如果未能解决你的问题,请参考以下文章
访问不允许SQL-Server列中的空值的INSERT或UPDATE(访问运行时错误3162)
在 iOS、PHP 和 JSON 中使用来自 SQL Server 的空值