I'm developing an ASP.NET Core 2.2 application that uses Entity Framework Core. I have a one-to-many relationship between OrganismoTransito
(parent) and RadicacionCuenta
(many). Following is the snippet code according the issue.
Entity Classes:
public partial class RadicacionCuenta
{
public RadicacionCuenta()
{
PropietariosRadicacionesCtas = new HashSet<PropietarioRadicacionCta>();
}
public int Id { get; set; }
public string Placa { get; set; }
public int IdTipoVehiculo { get; set; }
public int IdLinea { get; set; }
public string Modelo { get; set; }
public string Cilindraje { get; set; }
public string Tonelaje { get; set; }
public string Pasajeros { get; set; }
public string Watts { get; set; }
public DateTime FechaRegistro { get; set; }
public DateTime FechaSistema { get; set; }
public int IdOrganismoTransitoOrigen { get; set; }
public string EstadoDocumento { get; set; }
public string DocumentosFaltantes { get; set; }
public string NumeroFolios { get; set; }
public DateTime FechaDevolucion { get; set; }
public string UsuarioDevuelve { get; set; }
public string EstadoRegistro { get; set; }
public string MotivoCancelacion { get; set; }
public string TipoPublico { get; set; }
public string ValorFactura { get; set; }
public int IdOrganismoTransitoDestino { get; set; }
public virtual OrganismoTransito IdOrganismoTransitoDestinoNavigation { get; set; }
public virtual OrganismoTransito IdOrganismoTransitoOrigenNavigation { get; set; }
public virtual ICollection<PropietarioRadicacionCta> PropietariosRadicacionesCtas { get; set; }
}
public partial class OrganismoTransito
{
public OrganismoTransito()
{
Blindados = new HashSet<Blindado>();
Concesionarios = new HashSet<Concesionario>();
FilialesTransito = new HashSet<FilialTransito>();
Limitaciones = new HashSet<Limitacion>();
Polarizados = new HashSet<Polarizado>();
RadicacionesCuentaIdOrganismoTransitoDestinoNavigations = new HashSet<RadicacionCuenta>();
RadicacionesCuentaIdOrganismoTransitoOrigenNavigations = new HashSet<RadicacionCuenta>();
Vehiculos = new HashSet<Vehiculo>();
}
public int Id { get; set; }
public int IdDivisionPolitica { get; set; }
public string Nombre { get; set; }
public string Jurisdiccion { get; set; }
public string Categoria { get; set; }
public string Abreviatura { get; set; }
public string DivipoAlterno { get; set; }
public bool Activo { get; set; }
public virtual DivisionPolitica IdDivisionPoliticaNavigation { get; set; }
public virtual ICollection<Blindado> Blindados { get; set; }
public virtual ICollection<Concesionario> Concesionarios { get; set; }
public virtual ICollection<FilialTransito> FilialesTransito { get; set; }
public virtual ICollection<Limitacion> Limitaciones { get; set; }
public virtual ICollection<Polarizado> Polarizados { get; set; }
public virtual ICollection<RadicacionCuenta> RadicacionesCuentaIdOrganismoTransitoDestinoNavigations { get; set; }
public virtual ICollection<RadicacionCuenta> RadicacionesCuentaIdOrganismoTransitoOrigenNavigations { get; set; }
public virtual ICollection<Vehiculo> Vehiculos { get; set; }
}
I used Fluent configuration in the DbContext
's OnModelCreating
, I don't use any annotations. This is the code:
modelBuilder.Entity<RadicacionCuenta>(entity =>
{
entity.ToTable("RadicacionesCuentas");
entity.Property(e => e.Cilindraje)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.DocumentosFaltantes)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.EstadoDocumento)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.EstadoRegistro)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.FechaDevolucion).HasColumnType("datetime2(0)");
entity.Property(e => e.FechaRegistro).HasColumnType("datetime2(0)");
entity.Property(e => e.FechaSistema).HasColumnType("datetime2(0)");
entity.Property(e => e.Modelo)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.MotivoCancelacion)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.NumeroFolios)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.Pasajeros)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.Placa)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.TipoPublico)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.Tonelaje)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.UsuarioDevuelve)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.ValorFactura)
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.Watts)
.IsRequired()
.HasMaxLength(10)
.IsUnicode(false);
entity.HasOne(d => d.IdOrganismoTransitoDestinoNavigation)
.WithMany(p => p.RadicacionesCuentaIdOrganismoTransitoDestinoNavigations)
.HasForeignKey(d => d.IdOrganismoTransitoDestino)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_RadicaCuentas_OTransitoD");
entity.HasOne(d => d.IdOrganismoTransitoOrigenNavigation)
.WithMany(p => p.RadicacionesCuentaIdOrganismoTransitoOrigenNavigations)
.HasForeignKey(d => d.IdOrganismoTransitoOrigen)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_RadicaCuentas_OTransitoO");
});
When I try to run Add-Migration
in the Package Manager Console, I get this error:
Add-Migration InitialCreateVehiculosSchema -Context ApplicationDbContext -Project SicotX.Backend.EF -StartupProject SicotX -OutputDir Migrations
System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'OrganismoTransito.RadicacionesCuentaIdOrganismoTransitoDestinoNavigations' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder) at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.Validate() at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel() at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.b__1() at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy
1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.CreateValue() at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_2(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor
1.CreateValue() at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_2(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor
2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor
2.VisitCallSite( IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure
2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure
1 accessor) at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure1 accessor) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, Str ing outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Unable to determine the relationship represented by navigation property 'OrganismoTransito.RadicacionesCuentaIdOrganismoTransitoDestinoNavigations' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
I need help! Thanks
You have need to load to related data using Explicit Loading. You are not post your lambda expression query, so i am make simple lambda expression query using your table. please try the below method you will access parent table data from child table.
using (var context = new dbContext())
{
var varOrg = context.OrganismoTransito
.Single(b => b.Id == 1);
context.Entry(varOrg)
.Reference(b => b.RadicacionesCuentaIdOrganismoTransitoDestinoNavigations)
.Load();
}
For more infomation please read this article. https://docs.microsoft.com/en-us/ef/core/querying/related-data#explicit-loading
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.