我想对我的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个或更多映射枚举的情况下工作。
e.g。如果Child
具有Type
和Status
属性,我预计每Children
有8 Parent
个:
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,我可以自动化除循环之外的所有内容,以生成Enums的每个排列。 那就是我要问的怎么做。
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; }
}
答案 0 :(得分:1)
是的,这是可能的:
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;