gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:20 25 4
gpt4 key购买 nike

我正在尝试对某些响应 Entity Framework 数据库上下文的类进行单元测试。为了寻求帮助,我设法找到了一个名为 Effort 的库,它似乎有点旧,而且没有很好的文档记录,但它似乎可以工作,而且似乎很受欢迎。

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

当执行 ToArray() 时,我收到一个异常,提示 Sequence 不包含匹配的元素

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

一些片段:

[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

最佳答案

我刚遇到这个问题,我发现问题出在我使用的是 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

关于c# - 努力(EF 单元测试)给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43893911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com