Mapping issues in Microsoft.Entityframeworkcore

Asked

Viewed 163 times

1

I am trying to create in a class two Mappings Manytoone using the Entity Framework, but I’m not succeeding, if I take either of the two mappings and leave the other, it works but if I leave the two of the error. The project is being done in C# in visual studio, the type of project is UWP (Universal Windows Platform). I’m using Microsoftentityframeworkcore.Sqlite version 1.1.0 The database I’m modeling is Sqlite.

This is the error message I get:

System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'Sentence.FirstTerm' of type 'Term'. Either manually configure the relationship, or ignore this property from the model.
   em Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder)
   em Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder)
   em Microsoft.EntityFrameworkCore.Metadata.Internal.Model.Validate()
   em Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Validate()
   em Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   em Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass14_0.<GetModel>b__0(Object k)
   em System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   em Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   em Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   em Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   em Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   em Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_6(IServiceProvider p)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactoryService(FactoryService factoryService, ServiceProvider provider)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   em Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   em Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   em Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   em Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.<>c__DisplayClass6_0.<ConfigureContextServices>b__9(IServiceProvider _)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactoryService(FactoryService factoryService, ServiceProvider provider)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   em Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   em Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   em Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   em Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   em Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   em Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   em Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   em Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
   em Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   em Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to determine the relationship represented by navigation property 'Sentence.FirstTerm' of type 'Term'. Either manually configure the relationship, or ignore this property from the model.

This is my Term class:

namespace Test.Entity
{
    public class Term
    {
        public Term() {
            FirstTerms = new List();
            LastTerms = new List();
        }
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        protected int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [InverseProperty("FirstTerm")]
        public virtual ICollection FirstTerms { get; set; }

        [InverseProperty("LastTerm")]
        public virtual ICollection LastTerms { get; set; }
    }
}

This is my Sentence class:


namespace Test.Entity
{
    public class Sentence : Term
    {
        [ForeignKey("Rule")]
        public int RuleId { get; set; }
        public Rule Rule { get; set; }

        [ForeignKey("Operation")]
        public int OperationId { get; set; }
        public Operation Operation { get; set; }

        [ForeignKey("FirstTerm")]
        public int FirstTermId { get; set; }
        public virtual  Term FirstTerm { get; set; }
        [ForeignKey("LastTerm")]
        public int LastTermId { get; set; }
        public virtual Term LastTerm { get; set; }
    }
}

This is my context class (there is more content in it, but none of it refers to the Term or Sentence class):

 protected override void OnModelCreating(ModelBuilder modelBuilder) {
            modelBuilder.Entity().HasIndex(r => r.FirstTermId);
            modelBuilder.Entity().HasIndex(r => r.LastTermId);

        }

Error appears when I execute command:

Add-Migration Vanilla

If in class Sentence i remove the LastTerm and leave the FirstTerm or remove FirstTerm and leave LastTerm the Migration runs without error.

References of my project:

inserir a descrição da imagem aqui

How to solve this problem?

  • Both ForeignKey are related to Term seems incorrect to me.

  • This sentence is a mathematical sentence, in the example above it would be the first term and the last term and the mathematical operation, so I need both, this term can be a constant, for example it is another class that extends term, so I need both, but it may be that the notation is wrong, I accept suggestion.

  • Got this example from somewhere? Has the link?

  • No, it may be that the notation is wrong!

  • From what he tried to do, is it looking like he wants to make a tree structure on the bench, or am I wrong? Even so, what I noticed is that Attribute [Foreignkey("Operation")] is in the wrong property as well.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.