Base solution for your next web application
Open Closed

Testing Issues with Entities #658


User avatar
0
ninomartini created

When adding either of the following to an entity breaks the Test project.

public virtual DbGeography Location { get; set; }

or

[Column(TypeName = "Money")] public decimal MonthlyFee { get; set; }

I can handle using a decimal field in place of Money. However, I don't know how to get around DbGeography. Here is the exception when running ANY test:

System.Collections.Generic.KeyNotFoundException was unhandled by user code HResult=-2146232969 Message=The given key was not present in the dictionary. Source=mscorlib StackTrace: at System.Collections.Generic.Dictionary2.get_Item(TKey key) at Effort.Provider.EffortProviderManifest.GetStoreType(TypeUsage edmType) at System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.MapTableColumn(EdmProperty property, String columnName, Boolean isInstancePropertyOnDerivedType) at System.Data.Entity.ModelConfiguration.Edm.Services.PropertyMappingGenerator.Generate(EntityType entityType, IEnumerable1 properties, EntitySetMapping entitySetMapping, MappingFragment entityTypeMappingFragment, IList1 propertyPath, Boolean createNewColumn) at System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping) at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping) at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel) 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.RetryLazy2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at Abp.EntityFramework.AbpDbContext.Initialize() at Castle.MicroKernel.LifecycleConcerns.InitializationConcern.Apply(ComponentModel model, Object component) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.ApplyConcerns(IEnumerable1 steps, Object instance) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.ApplyCommissionConcerns(Object instance) at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden) at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally) at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden) at Castle.MicroKernel.Handlers.ExtendedHandler.InvokeResolvePipeline(Int32 extensionIndex, ResolveInvocation invocation) at Castle.MicroKernel.Handlers.ExtendedHandler.<>c__DisplayClass6.<InvokeResolvePipeline>b__5() at Castle.MicroKernel.Handlers.ResolveInvocation.Proceed() at Castle.MicroKernel.Handlers.ComponentLifecycleExtension.Intercept(ResolveInvocation invocation) at Castle.MicroKernel.Handlers.ExtendedHandler.InvokeResolvePipeline(Int32 extensionIndex, ResolveInvocation invocation) at Castle.MicroKernel.Handlers.ExtendedHandler.Resolve(CreationContext context, Boolean instanceRequired) at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context) at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy) at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy) at Castle.MicroKernel.DefaultKernel.Resolve(Type service, IDictionary arguments) at Castle.Windsor.WindsorContainer.Resolve[T]() at Abp.Dependency.IocManager.Resolve[T]() at FourMartinis.TracZu.Tests.AppTestBase.UsingDbContext(Action1 action) in D:\Documents\GitHub\TracZu\Tests\FourMartinis.TracZu.Tests\AppTestBase.cs:line 67 at FourMartinis.TracZu.Tests.AppTestBase..ctor() in D:\Documents\GitHub\TracZu\Tests\FourMartinis.TracZu.Tests\AppTestBase.cs:line 34 at FourMartinis.TracZu.Tests.Auditing.AuditLogAppService_Tests..ctor() in D:\Documents\GitHub\TracZu\Tests\FourMartinis.TracZu.Tests\Auditing\AuditLogAppService_Tests.cs:line 20 InnerException:

Thank you in advance.


9 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    This may be a restriction of Effort, but I did not try. Is it working with the regular web project? is DbGeography a complex type? can you share this class.

  • User Avatar
    0
    ninomartini created

    Thank you for the quick response.

    Yes, the web project is working without any issues.

    As for DbGeography, this is an Entity Framework spatial type. To use the DbGeography type, you must add a reference to the System.Data.Entity assembly and also add the System.Data.Spatial using statement.

    Again, thank you in advance.

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    OK, then it seems Effort probably does not support these. Can you create an issue on <a class="postlink" href="https://github.com/tamasflamich/effort/issues">https://github.com/tamasflamich/effort/issues</a> to ask it and request for support?

    If you really need to test this class, you should switch to another testing solution. Database testing is always hard. Effort is one of the simple solutions, that's why I implemented it. But it may have limitations (as every solution). You can find other possibilities if you search the web.

    Have a nice day.

  • User Avatar
    0
    dominici created

    Hi. I have the same problem and I tried for a long time to bypass it without success. I know that it is an issue of Effort but now I really need help.

    I would like to explore 2 different possibilities:

    • Use Effort but ignore DbGeography type. This seems possible reading the comment of jimmymain, but in aspnetzero I get the same exception. Below my code:
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    	base.OnModelCreating(modelBuilder);
    	modelBuilder.Entity<Report>().Ignore(_ => _.TestPosition);
    }
    
    • Change the effort library with an equivalent one. I searched on the web and it seems Moq library do the same thing of effort (see [https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113).aspx])). I tried to use Moq without success. The main problem is that I cannot understand how to get a db connection string as Effort do:
    _hostDb = DbConnectionFactory.CreateTransient();
    

    Moreover, Moq seems a little bit different than Effort. I will appreciate suggestions and code snippet to configure Moq or another in-memory db library

    Can you help me?

    Thanks Fabrizio Dominici

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    The first solution seems to be working. Do you have any other DbGeography type field other than TestPosition in your entities ? I think you need to ignore all DbGeography type fields, otherwise all test will be failed as well I think.

    If you don't have any other field than TestPosition, can you send your project to <a href="mailto:[email protected]">[email protected]</a> ?

    Thanks.

  • User Avatar
    0
    dominici created

    Hi, no, I commented each DbGeography type except TestPosition. Unfortunately I cannot share my project with you for company policy. Please can you share with me the project you tested that ignore the DbGeography type? (<a href="mailto:[email protected]">[email protected]</a>) I have version 3.3.0 of aspnetzero core angular with .NET Framework 4.6.1. By the way, I tried to download a new aspnetzero core angular project 3.3.0 to create a basic test project with only an entity with a DbGeography problem but now it seems full asp.net core. Is it right? And it seems there is not DbGeography in entity framework (core version). I have had a lot of problems trying to migrate from asp.net framework to asp.net core and also from visual studio 2015 to visual studio 2017.

    Anyway, can you change Effort with another library? Because I need to test also geospatial queries. Can you change Effort with another library? This is the best thing for me and for each person that buy aspnetzero I think.

    Thanks a lot Fabrizio Dominici

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I will send you the sample project on monday. The verison 3.3 and 3.4 does not use .net core and entityframework core yet. We will release 4.0 with .Net Core and EF Core in a few weeks and that version does not use Effort. But EF Core does not contain DbGeography yet, there is an issue but it is open for a very long time <a class="postlink" href="https://github.com/aspnet/EntityFramework/issues/1100">https://github.com/aspnet/EntityFramework/issues/1100</a>.

    It is hard to remove Effort for the current versions of AspNet Zero. In your case maybe it is better to use a real db for each test, what do you think ?

  • User Avatar
    0
    dominici created

    To be honest I don't like so much the idea to use a real db, because is very convenient to use an in memory db. I think asp net zero is a very powerful framework but this is a big limitation in a moment in which geospatial data are used a lot. In all my projects I need to do geospatial query. So I hope you consider with high priority to change effort with an equivalent library that support DbGeography. I tried to change effort by myself but for me is very hard because I have not a deep knowledge of asp net zero.

    So, let's try to summarize what are the possibilities at the state of the art of asp net zero, keeping in mind that I need DbGeography:

    • I cannot use the asp net zero core because DbGeography is not supported by entity framework core. So I must use asp net zero .net framework
    • I could use DbGeography and a real db for testing. In this case do I have only to set the connection string to the real db in AppTestBase class here
    _hostDb = DbConnectionFactory.CreateTransient();
    

    and everything work?

    • I could continue to use effort ignoring DbGeography type. By the way I did't received your test code. Can you send me it?

    You say asp net zero 4.0 won't use Effort. What library it will use for in memory testing?

    Thanks for your support Fabrizio Dominici

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I understand your case. We will use Entity Framewor In Memory DB for AspNet Zero 4.0 tests.

    Currently, Abp.Dapper package has a similar tests which you can use in your case. It simply creates a local db before each test and uses it for testing.

    You just need to create a different database before each of your tests, maybe using a guid as db name. You can check it here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/tree/dev/test/Abp.Dapper.Tests">https://github.com/aspnetboilerplate/as ... pper.Tests</a>.

    Please let us know if this does not work for you.

    Thanks.