【问题标题】:Many-to-Many Relationship Between Enum and Entity is Not Being Stored未存储枚举和实体之间的多对多关系
【发布时间】:2017-03-27 14:29:38
【问题描述】:

我正在开发一个实体框架代码优先项目,其中我有多对多关系:

服务提供者可以有很多服务类型,服务类型可以有很多服务提供者。

服务只有一种服务类型。

服务类型是一个枚举:

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

ServiceProviderViewModel

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
    {
        ...
    }
}

}

最后是ServiceProviderController

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

问题是;数据库中没有用于添加的服务类型的表或列。在调试时,我可以看到复选框的选定值被转换为相应的枚举并添加到 serviceProvider.ServiceTypes

当我尝试获取服务类型时什么都没有:

repository.ServiceProviders.ServiceTypes

我错过了什么?

【问题讨论】:

  • 您应该将ServiceType 转换为映射到(至少)具有 Id(主键)和名称的数据库表的实体类型。它还使您可以灵活地添加/删除服务类型,而无需重新编译代码。

标签: asp.net-mvc entity-framework enums ef-code-first many-to-many


【解决方案1】:

枚举是一个结构。只有类类型可以被持久化。实体框架正确地忽略了“关系”。你如何想象一个枚举首先会被表示为一个数据库表?

【讨论】:

  • 谢谢,但是如果关系是一对多的,它会被存储在一个列中。据我所知,较新的 ASP.NET MVC 版本确实支持枚举...?
  • 支持枚举,但作为单列类型。您不能拥有枚举集合,因为这需要一个单独的表来存储这些枚举值的“行”。请记住,关系数据库必须扁平化对象层次结构。您可以拥有一个将该枚举作为属性的实体,然后与该 entity 进行一对多,但不能直接使用枚举。
  • EF 没有理由不能或不应该支持这一点。您可以只使用联结表并将枚举值用作第二列键 - 没有“枚举”表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
相关资源
最近更新 更多