努力(EF单元测试)出错 [英] Effort (EF Unit Testing) giving errors
问题描述
我正在尝试对某些在Entity Framework DB Context上答复的类进行单元测试.为了寻求帮助,我设法找到了一个名为Effort的库,该库似乎有点旧,并且没有很好的文档记录,但是它似乎可以正常运行,并且很受欢迎.
I am trying to do unit testing on some classes that reply on an Entity Framework DB Context. For help, I managed to find a library called Effort, which seems to be a little old, and not very well documented, but it seems to work, and seems to be quite popular.
我正在尝试使用CSV数据加载器.
I am trying to use a CSV data loader.
当执行 ToArray()
时,我收到一个异常,提示 Sequence不包含匹配的元素
.
When doing a ToArray()
I receive an exceptions saying Sequence contains no matching element
.
关于我可能做错了什么的任何想法?还是如果我不是一个不同的图书馆,我可能想给一个机会?
Any ideas on what I might be doing incorrectly? Or if not a different library I might want to give a chance?
一些摘要:
[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:
The 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:
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
的模型构建器约定时,我仍然遇到完全相同的错误.我猜您的其中一个实体具有该数据注释(或约定)?
I've just had this issue and I discovered the problem was that I was using the ColumnType
data annotation. Even when I changed to using the model builder convention of HasColumnType
, I still got the exact same error. I'm guessing one of your entities has that data annotation (or convention)?
如果是这种情况,最简单的解决方法是删除数据注释.如果不可能,我在测试中覆盖的DbContext上创建了一个虚拟属性(例如 public virtual bool IsInMemoryContext {get;} = false;
)- public overlay bool IsInMemoryContext {get;} = true;
,并在添加列类型之前,在 OnModelCreating
方法中检查了是否设置了此属性.
If that's the case, the simplest fix would be to remove the data annotation. If not possible, I created a virtual property (e.g. public virtual bool IsInMemoryContext { get; } = false;
) on the DbContext that I overrode in my tests - public override bool IsInMemoryContext { get; } = true;
, and in the OnModelCreating
method, I checked if this property was set, before adding the column types.
if (!IsInMemoryContext)
{
modelBuilder.Entity<AuditLog>()
.Property(e => e.EventType)
.HasColumnType("char");
}
有关该错误的更多信息,请参见: Effort上的GitHub问题
More information on the error can be found here: GitHub issue with Effort
这篇关于努力(EF单元测试)出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!