Asp.net mvc 未存储枚举和实体之间的多对多关系

Asp.net mvc 未存储枚举和实体之间的多对多关系,asp.net-mvc,entity-framework,enums,ef-code-first,many-to-many,Asp.net Mvc,Entity Framework,Enums,Ef Code First,Many To Many,我正在从事一个实体框架代码优先项目,在该项目中我有多对多关系: 服务提供者可以有许多服务类型,而服务类型可以有许多服务提供者 服务只有一种服务类型 服务类型是一个枚举: public enum ServiceTypeEnum { Ambulance = 1, [Display(Name = "Cash Advance")] CashAdvance = 2, Hospitalization = 3, Hotel = 4, [Display(Name

我正在从事一个实体框架代码优先项目,在该项目中我有多对多关系:

服务提供者可以有许多服务类型,而服务类型可以有许多服务提供者

服务只有一种服务类型

服务类型是一个枚举:

public enum ServiceTypeEnum
{
    Ambulance = 1,
    [Display(Name = "Cash Advance")]
    CashAdvance = 2,
    Hospitalization = 3,
    Hotel = 4,
    [Display(Name = "House Call")]
    HouseCall = 5,
    [Display(Name = "Medical Escort")]
    MedicalEscort = 6,
    Transfer = 7,
    Repatriation = 8
}
服务提供商

public partial class ServiceProvider
{
    public ServiceProvider()
    {
        ServiceTypes = new HashSet<ServiceTypeEnum>();
    }

    [Key]
    public int ServiceProviderID { get; set; }

    [Required]
    [StringLength(100)]
    public string Title { get; set; }

    // This is OK for a single Service 
    //public virtual ServiceTypeEnum ServiceType { get; set; }

    // I added this so that Service Providers can have multiple Service Types
    public ICollection<ServiceTypeEnum> ServiceTypes { get; set; }

    public IEnumerable<Service> Services { get; set; } 
}

我缺少什么?

枚举是一个结构。只能持久化类类型。实体框架正确地忽略了“关系”。您认为一个enum首先应该如何表示为一个数据库表?

谢谢,但是如果关系是一对多,它将存储在一个列中。据我所知,较新的ASP.NET MVC版本确实支持枚举…?枚举是受支持的,但作为单个列类型。您不能拥有枚举集合,因为这需要一个单独的表来存储这些枚举值的“行”。记住,关系数据库必须扁平化对象层次结构。您可以将该枚举作为属性使用一个实体,然后使用该实体使用一对多,但不能直接使用枚举。EF没有理由不能或不应该支持这一点。您可以只使用连接表并将枚举值用作第二列键,而不使用“enum”表。您应该将
ServiceType
转换为映射到具有(至少)Id(主键)和名称的数据库表的实体类型。它还使您能够灵活地添加/删除服务类型,而无需重新编译代码。
public class ServiceProviderViewModel
{
    public class CreateModel
    {
        public int ServiceProviderID { get; set; }

        [Required]
        [StringLength(100)]
        [Display(Name = "Title")]
        public string Title { get; set; }

        public IEnumerable<KeyValuePair<string, int>> AllServiceTypes { get; set; }
        public string[] SelectedServiceTypes { get; set; }

        //public ServiceTypeEnum ServiceType { get; set; }
    }

    public class EditModel
    {
        ...
    }
}
 public ActionResult Create()
    {
        var _allServiceTypes =  Enum.GetValues(typeof(ServiceTypeEnum))
           .Cast<ServiceTypeEnum>()
           .Select(t => new KeyValuePair<string, int>(t.ToString(), (int) t));

        var viewModel = new ServiceProviderViewModel.CreateModel()
        {
            AllServiceTypes = _allServiceTypes
        };

        return View(viewModel);
    }

    [HttpPost]
    public ActionResult Create(ServiceProviderViewModel.CreateModel viewModel)
    {
        if (ModelState.IsValid)
        {
            // This is OK for a single Service Type
            //var serviceProvider = new ServiceProvider
            //{
            //    Title = viewModel.Title,
            //    ServiceType = viewModel.ServiceType
            //};

            // For multiple Service Types
            var serviceProvider = new ServiceProvider();
            serviceProvider.Title = viewModel.Title;
            for (int i = 0; i < viewModel.SelectedServiceTypes.Length; i++)
            {
                serviceProvider.ServiceTypes.Add((ServiceTypeEnum)Enum.Parse(typeof(ServiceTypeEnum), viewModel.SelectedServiceTypes[i].ToString()));
            }

            repository.InsertServiceProvider(serviceProvider);


            repository.Save();
            return RedirectToAction("Index");
        }
        return View(viewModel);
    }
repository.ServiceProviders.ServiceTypes