努力(EF单元测试)给出错误

时间:2017-05-10 13:28:13

标签: c# entity-framework unit-testing effort

我正在尝试对一些回复实体框架数据库上下文的类进行单元测试。为了获得帮助,我设法找到了一个名为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

1 个答案:

答案 0 :(得分:3)

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

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

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

有关错误的更多信息,请访问:GitHub issue with Effort