AutoFixture with Entity Framework - 将 int 映射到枚举

本文关键字:int 映射 枚举 with Entity Framework AutoFixture | 更新日期: 2023-09-27 18:04:19

我想对我的实体框架(6.1.3(数据模型使用AutoFixture(3.30.8(。我已经实现了 AutoFixture.AutoEF (0.3.5( 包来帮助修复实体并避免关系生成的循环引用。

但是,我的表有几个 int 列,这些列由代码中的枚举表示,我希望能够根据枚举值设置 int 值,并为每个枚举值设置一个代理类。

这是我的架构的一个简化示例:

public partial class MyContext : DbContext
{
    public virtual DbSet<Parent> Parents { get; set; }
    public virtual DbSet<Child> Children { get; set; }
}
public partial class Parent
{
    public Parent()
    {
        this.Children = new HashSet<Child>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public int Status { get; set; }
    public virtual ICollection<Child> Children { get; set; }
}
public partial class Child
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
    public int Type { get; set; }
    public virtual Parent Parent { get; set; }
}

这是我的枚举:

public enum Status
{
    Active = 1,
    Inactive = 2
}
public enum Type
{
    Up = 1,
    Down = 2,
    Left = 3,
    Right = 4
}

以下是我创建代理的方式:

var fixture = new Fixture();
fixture.Customize(new EntityCustomization(new DbContextEntityTypesProvider(typeof(MyContext))));
var parents = fixture.CreateMany<Parent>();

这在我有一个 3 个Parent类的集合中,每个类都有 3 个Child类,并且 Id 属性很好地匹配。但是,正如预期的那样,StatusType 属性是由 AutoFixture 生成的随机整数。

我想要的是 2 个Parent类,一个有1 Status,一个有Status 2,每个班级都有 4 个Child类,每个班级都有 1234Type

使用自动固定功能可以自动执行此操作吗?

编辑:为了更清楚地说明我在问什么:

如何自动将类上的 int 属性映射到枚举,以便为映射枚举的每个值获取一个代理类。

这还需要在一个类有 2 个或更多映射枚举的情况下工作。

例如,如果ChildTypeStatus属性,我希望每Parent 8 Children

Status = 1, Type = 1
Status = 1, Type = 2
Status = 1, Type = 3
Status = 1, Type = 4
Status = 2, Type = 1
Status = 2, Type = 2
Status = 2, Type = 3
Status = 2, Type = 4

进一步推断,如果Parent同时具有StatusType我预计有 8 个Parent代理类,每个代理类有 8 个Child代理类。

编辑2:这是我手动编码的替代生成器的外观示例,如果我将两个枚举都放在两个类上。通过使用AutoFixture,我可以自动化除循环之外的所有内容,以生成枚举的每个排列。这就是我要问如何做的。

public class Substitutes
{
    private int parentIdSeed;
    private int childIdSeed;
    public Substitutes()
    {
        this.parentIdSeed = 0;
        this.childIdSeed = 0;
        this.Parents = new List<Parent>();
        this.Children = new List<Child>();
        this.GenerateParents();
    }
    private void GenerateParents()
    {
        foreach (Type type in Enum.GetValues(typeof(Type)))
        {
            foreach (Status status in Enum.GetValues(typeof(Status)))
            {
                this.parentIdSeed++;
                var parent = new Parent { Id = this.parentIdSeed, Name = "Parent " + this.parentIdSeed, Status = (int)status, Type = (int)type };
                this.GenerateChildren(parent);
                this.Parents.Add(parent);
            }
        }
    }
    private void GenerateChildren(Parent parent)
    {
        foreach (Type type in Enum.GetValues(typeof(Type)))
        {
            foreach (Status status in Enum.GetValues(typeof(Status)))
            {
                this.childIdSeed++;
                var child = new Child { Id = this.childIdSeed, Name = "Child " + this.childIdSeed, Status = (int)status, Type = (int)type, Parent = parent, ParentId = parent.Id };
                parent.Children.Add(child);
                this.Children.Add(child);
            }
        }
    }
    public List<Child> Children { get; set; }
    public List<Parent> Parents { get; set; }
}

AutoFixture with Entity Framework - 将 int 映射到枚举

是的,这是可能的:

var parents = fixture.CreateMany<Parent>(2).ToList();
parents[1].Status = 1;
parents[1].Children = fixture.CreateMany<Child>(4).ToList();
parents[1].Children.ElementAt(1).Type = 1;
parents[1].Children.ElementAt(2).Type = 2;
parents[1].Children.ElementAt(3).Type = 3;
parents[1].Children.ElementAt(4).Type = 4;
parents[2].Status = 2;
parents[2].Children = fixture.CreateMany<Child>(4).ToList();
parents[2].Children.ElementAt(1).Type = 1;
parents[2].Children.ElementAt(2).Type = 2;
parents[2].Children.ElementAt(3).Type = 3;
parents[2].Children.ElementAt(4).Type = 4;