使用EF核心的asp.net核心MVC项目,我有一个名为Message的对象,该对象具有一个称为Service的ENUM。尝试将消息添加到数据库(SQL Server)时,它保持为0。
有任何想法吗?
讯息类别:
using System;
using System.ComponentModel.DataAnnotations;
namespace AutoMatcher.Models
{
public class Message
{
[Key]
public int MessageId { get; set; }
public string UserId { get; set; }
public int Likes { get; set; }
public Service Service { get; set; }
public DateTime Time { get; set; }
public ApplicationUser User { get; set; }
public Message()
{
}
}
}
枚举
namespace AutoMatcher.Models
{
public enum Service
{
Badoo ,
Tinder,
Grinde
}
}
AppDbContext:
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace AutoMatcher.Models
{
public class AppDbContext : IdentityDbContext<ApplicationUser>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Message>().Property(m => m.Service).HasConversion<int>();
builder.Entity<ApplicationUser>().HasMany<Message>(m => m.Messages).WithOne(u => u.User).IsRequired();
base.OnModelCreating(builder);
}
public DbSet<Message> Messages { get; set; }
}
}
移民:
migrationBuilder.CreateTable(
name: "Messages",
columns: table => new
{
MessageId = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
UserId = table.Column<string>(nullable: false),
Likes = table.Column<int>(nullable: false),
Service = table.Column<int>(nullable: false),
Time = table.Column<DateTime>(nullable: false)
},
这是提交我想要的数据的形式:
@model Message
@using Models
@inject UserManager<ApplicationUser> signIn
@{
ViewData["Title"] = "Dashboard";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Dashboard @ViewBag._userId </h1>
<form asp-action="Schedule" asp-controller="Actions">
<input asp-for="Likes">
<select asp-items="Html.GetEnumSelectList<Service>()"></select>
<input type="datetime-local" asp-format="{0:yyyy-MM-dd}" class="form-control" />
<button type="submit">Start</button>
</form>
这是添加后的数据库快照:
缺少asp
<select asp-for="Service" asp-items="Html.GetEnumSelectList<Service>()"></select>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。