努力(EF單元測試)給出錯誤

c# effort entity-framework unit-testing

我試圖對一些實體框架數據庫上下文回复的類進行單元測試。為了獲得幫助,我設法找到了一個名為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; }
}

DB上下文:

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

熱門答案

我剛剛遇到這個問題,我發現問題在於我正在使用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與Effort的問題



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow