Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF core表不会分析枚举并保持在0上_C#_Sql Server_Asp.net Core Mvc_Entity Framework Core - Fatal编程技术网

C# EF core表不会分析枚举并保持在0上

C# EF core表不会分析枚举并保持在0上,c#,sql-server,asp.net-core-mvc,entity-framework-core,C#,Sql Server,Asp.net Core Mvc,Entity Framework Core,使用EF core的asp.net core MVC项目, 我有一个名为Message的对象,它有一个名为Service的枚举。 尝试将消息添加到数据库(SQL server)时,它保持在0上 有什么想法吗 消息类: using System; using System.ComponentModel.DataAnnotations; namespace AutoMatcher.Models { public class Message { [Key]

使用EF core的asp.net core MVC项目, 我有一个名为Message的对象,它有一个名为Service的枚举。 尝试将消息添加到数据库(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; }
    }
}
使用Microsoft.AspNetCore.Identity.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore;
名称空间自动机模型
{
公共类AppDbContext:IdentityDbContext缺少的asp

<select asp-for="Service" asp-items="Html.GetEnumSelectList<Service>()"></select>


缺少的asp

<select asp-for="Service" asp-items="Html.GetEnumSelectList<Service>()"></select>


我通常会处理这样的基于枚举的选择列表

向视图模型添加一些额外属性,不要将DB模型用于视图!

public IEnumerable<SelectListItem> YourEnumSelectListProperty { get; set; }
public int SelectedItemId { get; set; }
用于一般设置枚举选择列表的一些辅助方法

public static IEnumerable<SelectListItem> GetGenericEnumSelectList<T>()
{
    return (Enum.GetValues(typeof(T)).Cast<Enum>().Select(e => new SelectListItem() { Text = EnumExtensions.GetEnumDescription(e), Value = Convert.ToInt32(e).ToString() })).ToList();
}


public static string GetEnumDescription(Enum value)
{
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                    typeof(DescriptionAttribute),
                    false);

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;

                return value.ToString();
 }
public静态IEnumerable GetGenericEnumSelectList()
{
return(Enum.GetValues(typeof(T)).Cast().Select(e=>newselectListItem(){Text=EnumExtensions.GetEnumDescription(e),Value=Convert.ToInt32(e.ToString()})).ToList();
}
公共静态字符串GetEnumDescription(枚举值)
{
FieldInfo fi=value.GetType().GetField(value.ToString());
DescriptionAttribute[]属性=
(DescriptionAttribute[])fi.GetCustomAttributes(
类型(描述属性),
假);
如果(属性)=null&&
属性(长度>0)
返回属性[0]。说明;
返回值.ToString();
}

我通常会处理这样的基于枚举的选择列表

向视图模型添加一些额外属性,不要将DB模型用于视图!

public IEnumerable<SelectListItem> YourEnumSelectListProperty { get; set; }
public int SelectedItemId { get; set; }
用于一般设置枚举选择列表的一些辅助方法

public static IEnumerable<SelectListItem> GetGenericEnumSelectList<T>()
{
    return (Enum.GetValues(typeof(T)).Cast<Enum>().Select(e => new SelectListItem() { Text = EnumExtensions.GetEnumDescription(e), Value = Convert.ToInt32(e).ToString() })).ToList();
}


public static string GetEnumDescription(Enum value)
{
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                    typeof(DescriptionAttribute),
                    false);

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;

                return value.ToString();
 }
public静态IEnumerable GetGenericEnumSelectList()
{
return(Enum.GetValues(typeof(T)).Cast().Select(e=>newselectListItem(){Text=EnumExtensions.GetEnumDescription(e),Value=Convert.ToInt32(e.ToString()})).ToList();
}
公共静态字符串GetEnumDescription(枚举值)
{
FieldInfo fi=value.GetType().GetField(value.ToString());
DescriptionAttribute[]属性=
(DescriptionAttribute[])fi.GetCustomAttributes(
类型(描述属性),
假);
如果(属性)=null&&
属性(长度>0)
返回属性[0]。说明;
返回值.ToString();
}

您没有将选择列表的选定值设置为int。创建一个视图模型并添加另一个用于保存该值的属性。您没有将选择列表的选定值设置为int。创建一个视图模型并添加另一个用于保存该值的属性。这不是答案,但仍然是一个很好的答案!I ag我不应该对视图使用DB模型,谢谢!这不是答案,但仍然是一个好答案!我同意我不应该对视图使用DB模型,谢谢!
public static IEnumerable<SelectListItem> GetGenericEnumSelectList<T>()
{
    return (Enum.GetValues(typeof(T)).Cast<Enum>().Select(e => new SelectListItem() { Text = EnumExtensions.GetEnumDescription(e), Value = Convert.ToInt32(e).ToString() })).ToList();
}


public static string GetEnumDescription(Enum value)
{
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                    typeof(DescriptionAttribute),
                    false);

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;

                return value.ToString();
 }