我想在代码第一应用程序中使用枚举作为外键。由于枚举存储为int,我认为我可以在枚举属性上使用属性[forkswey],但它抛出此异常:

The types of all properties in the Dependent Role of a referential constraint 
must be the same as the corresponding property types in the Principal Role
.

这里是我想要做的例子:

public enum UserType
{
    Administrator = 1,
    Member = 2
}

public class User
{
    public int UserId { get; set; }
    public string Login { get; set; }

    [ForeignKey("TypeDetails")]
    public UserType Type { get; set;}

    public virtual MasterType TypeDetails { get; set; }
}

public class MasterType
{
    public int MasterTypeId { get; set; }
    public string Description { get; set; }
    ...
}
.

是否可以通过流利的API或迁移来执行此操作或类似的东西?

感谢

有帮助吗?

解决方案

这是我之前的一个: https://www.nuget.org/包/ ef-枚举到查找

是一个核桃包,提供了一种方法,您可以在您的Seed(初始化程序和/或迁移)中调用,这将自动构建查找表并添加使用枚举的福布。用法信息

享受:-)如果它适用于您(或其他任何人!),请告诉我!)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top