1

我正在尝试对一些回复实体框架数据库上下文的类进行单元测试。寻求帮助,我设法找到了一个名为 Effort 的库,它似乎有点旧,文档也不是很好,但它似乎可以工作,而且似乎很受欢迎。

我正在尝试使用 CSV 数据加载器。

做的时候ToArray()我收到一个例外说Sequence contains no matching element

关于我可能做错了什么的任何想法?或者,如果不是不同的图书馆,我可能想给一个机会?

几个片段:

[Table("SEC_USER")]
public class SecUser {
    [Key][Column("USERID")]
    public int UserId { get; set; }

    [Column("USERNAME")]
    public string UserName { get; set; }
}

数据库上下文:

public class MusketeerDbContext : DbContext
{
    public virtual IDbSet<IbsCommunity> Communities { get; set; }
    public virtual IDbSet<IbsFunctionLinkLocation> Functionlinklocations { get; set; }
    public virtual IDbSet<IbsInstance> Instances { get; set; }
    public virtual IDbSet<SecUser> Users { get; set; }
    public virtual IDbSet<IbsFieldType> FieldTypes { get; set; }
    public virtual IDbSet<IbsLink> Links { get; set; }
    public virtual IDbSet<IbsFieldFll> FieldFlls { get; set; }
    public virtual IDbSet<IbsFieldValue> FieldValues { get; set; }

    public MusketeerDbContext() : base("name=EGS.My.MySettings.Conn") { }
    public MusketeerDbContext(DbConnection connection) : base(connection, true) { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("SA");
        modelBuilder.Conventions.Add(new FunctionConvention(typeof(OracleFunctions)));

        modelBuilder.Entity<IbsFieldValue>()
            .HasMany(fv => fv.InstancesFieldValues)
            .WithMany(i => i.InstancesFieldValues)
            .Map(mm =>
            {
                mm.MapLeftKey("FIELDVALUEID");
                mm.MapRightKey("INSTANCEID");
                mm.ToTable("IBS_INSTANCEFIELDVALUE");
            });
    }
}

public static class OracleFunctions
{
    [Function(FunctionType.BuiltInFunction, "TO_CHAR")]
    public static string ToChar(this int value) => Function.CallNotSupported<string>();

    [Function(FunctionType.BuiltInFunction, "TO_NCHAR")]
    public static string ToChar(this string value) => Function.CallNotSupported<string>();
}

SEC_USER.csv:

USERID,USERNAME
"1","Jonathan"

考试:

var path = @"C:\...\CSVs";
var dataLoader = new Effort.DataLoaders.CsvDataLoader(path);
var context = Effort.DbConnectionFactory.CreateTransient(dataLoader);
db = new MusketeerDbContext(context);
var users = db.Users.ToArray();

System.InvalidOperationException:

Message: "Sequence contains no matching element"
InnerException: null
StackTrace:
    at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
    at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name)
    at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty column, EntityType table, DbProviderManifest providerManifest)
    at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty column, EntityType table, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)
    at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.<>c__DisplayClass4.<Configure>b__3(Tuple`2 pm)
    at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
    at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)
    at System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride)
    at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride)
    at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
    at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, ICollection`1 entitySets, DbProviderManifest providerManifest)
    at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
    at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
    at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
    at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
    at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
    at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
    at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
    at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
    at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
    at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
    at Igs.Musketeer.DbRepository.Tests.SecurityRepositoryTest..ctor() in C:\Users\me\Source\Repos\Musketeer\Igs.Musketeer.DbRepositoryTests\SecurityRepositoryTest.cs:line 21
4

1 回答 1

3

我刚遇到这个问题,我发现问题是我正在使用ColumnType数据注释。即使我更改为使用 的模型构建器约定HasColumnType,我仍然遇到完全相同的错误。我猜你的一个实体有那个数据注释(或约定)?

如果是这种情况,最简单的解决方法是删除数据注释。如果不可能,我public virtual bool IsInMemoryContext { get; } = false;在我的测试中覆盖的 DbContext 上创建了一个虚拟属性(例如 )public override bool IsInMemoryContext { get; } = true;,并且在OnModelCreating方法中,我在添加列类型之前检查了该属性是否已设置。

if (!IsInMemoryContext)
{
    modelBuilder.Entity<AuditLog>()
        .Property(e => e.EventType)
        .HasColumnType("char");
}

可以在此处找到有关该错误的更多信息:GitHub issue with Effort

于 2017-10-17T14:21:11.900 回答