0

我在帮助应用程序上工作,它将为我的数据库生成示例数据。我决定使用AutoPoco,但是当我设置AutoPocoContainer填充集合时出现问题。

实体

我的实体在NHibernate中使用

 public partial class Project
 {
    public virtual string Title { get; set; }
    public virtual System.DateTime CreatedAt { get; set; }
    public virtual string Description { get; set; }

    private IList<ProjectMember> _projectMembers = new List<ProjectMember>();

    public virtual IList<ProjectMember> ProjectMembers
    {
      get { return _projectMembers; }
      set { _projectMembers = value; }
    }
}

public partial class ProjectMember
{
    public virtual bool isConfirmed { get; set; }
    public virtual string ProjectPosition { get; set; }
    public virtual Project Project { get; set; }
}

配置

这是我的配置的一部分(仅ProjectProjectMember):

IGenerationSessionFactory pocoFactory = AutoPocoContainer.Configure(x =>
{
    x.Conventions(c => c.UseDefaultConventions());
    x.AddFromAssemblyContainingType<CDSUser>();

    // ...
    x.Include<Project>()
        .Setup(c => c.Title).Use<ProjectTitleSource>()
        .Setup(c => c.CreatedAt).Use<DateTimeSource>(new DateTime(2015, 2, 1), new DateTime(2015, 6, 30))
        // here is problem
        .Setup(c => c.ProjectMembers).Collection(1, 10) // if I remove this line everything works
        .Setup(c => c.Description).Use<LoremIpsumSource>();
    x.Include<ProjectMember>()
        .Setup(c => c.isConfirmed).Value(true)
        .Setup(c => c.ProjectPosition).Value(string.Empty)
        .Setup(c => c.Project).FromParent();

    // ...
});

这是我尝试生成项目时的步骤:

var projects = fixture.List<Project>(100).Get();

一切都编译。

错误

当我运行它时,抛出异常:

System.MissingMethodException was unhandled
  HResult=-2146233069
  Message=Cannot create an instance of an interface.
  Source=mscorlib
  StackTrace:
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Activator.CreateInstance(Type type)
       at AutoPoco.DataSources.FlexibleEnumerableSource`3.AutoPoco.Engine.IDatasource.Next(IGenerationContext context)
       at AutoPoco.Engine.ObjectPropertySetFromSourceAction.Enact(IGenerationContext context, Object target)
       at AutoPoco.Engine.ObjectBuilder.EnactActionsOnObject(IGenerationContext context, Object createdObject)
       at AutoPoco.Engine.ObjectBuilder.CreateObject(IGenerationContext context)
       at AutoPoco.Engine.ObjectGenerator`1.Get()
       at AutoPoco.Engine.CollectionContext`2.<Get>b__2(IObjectGenerator`1 x)
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at AutoPoco.Engine.CollectionContext`2.Get()

       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

约束是我不能更改实体,因为它们是由外部工具生成的。

所以我的问题是:

  1. 是否可以生成(使用 AutoPoco)项目列表,其中每个项目都包含ProjectMembers

  2. 也许有更好的工具来生成适用于我的实体的数据?

我知道我可以首先生成项目列表,然后生成列表ProjectMembers然后将这些集合绑定在一起。但是将所有内容都配置在一个地方确实很诱人。

PS。我选择的主要原因AutoPoco是它可以让您真正轻松地创建新的数据源,例如FirstNameSourceProjectTitleSource等等。

编辑 1:收集的 AutoPoco 扩展如何:https ://github.com/hvitorino/AutoPoco/blob/master/AutoPoco/StandardExtensions.cs

4

1 回答 1

0

我认为您想要实现的目标在您拥有的配置中是不可能的。简而言之:FromParent() 并未被设计为在您尝试使用它时使用。FromParent() 会起作用,如果你有以下处置:

class Project
{
  public int ProjectId {get;set;}
  public string Title { get; set; }
  public DateTime CreatedAt { get; set; }
  public IList<ProjectMember> ProjectMembers { get; set; }
}

class ProjectMember
{
  public int ProjectId {get;set;}
  public bool isConfirmed { get; set; }
  public string ProjectPosition { get; set; }
}

在这种情况下,对 ProjectMember.ProjectId 的 FromParent() 调用将遍历父对象的生成上下文,并在 Project 对象上获取具有相同名称的类型的属性,并且您的代码将起作用。但是,唉,它没有通过。:)

于 2016-08-05T18:27:25.297 回答