我想针对我的 Entity Framework (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 属性很好地匹配。但是,正如预期的那样,Status
和 Type
属性是由 AutoFixture 生成的随机整数。
我想要的是 2 个 Parent
类,一个的 Status
为 1
,另一个为 Status
的 2
并且每个都有 4 个 Child
类,每个类都有 Type
的 1
、2
、3
和 4
。
这是否可以使用 AutoFixture 自动实现?
编辑:为了更清楚地说明我的问题:
我怎样才能自动将我的类的一个 int 属性映射到一个枚举,以便我为映射的枚举的每个值获得一个代理类。
这也需要在类具有 2 个或更多映射枚举的情况下工作。
例如如果 Child
具有 Type
和 Status
属性,我希望每个 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
也有 Status
和 Type
我希望有 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; }
}
最佳答案
是的,这是可能的:
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;
关于c# - 带有 Entity Framework 的 AutoFixture - 将 int 映射到枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586964/