Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
EF Core HasOptional WithOptionalDependent

I have the following tables in my database:

Product                                 ResourceAssociation
-------                                 ------------------
Name                          /-------> Id
ResourceAssociation_Id ------/         SomeProperty

So Product has a ResourceAssociation_Id, which is a FK to the ResourceAssociation table.

This is a legacy application that had an Entity Framework 6 map that contained this piece of code:

HasOptional(t => t.ResourceAssociation).WithOptionalDependent().WillCascadeOnDelete(true);

I'm looking to port this to EF Core. EF Core doesn't have the HasOptional anymore, it seems. I've tried this:

builder.HasOne(t => t.ResourceAssociation)
    .WithOne().HasForeignKey("ResourceAssociation_Id")
    .IsRequired(false)
    .OnDelete(DeleteBehavior.Cascade);

I've also tried replacing the WithOne with WithMany. But it doesn't work.

When I try to create a new Product, without a ResourceAssociation (because it isn't required), EF Core still tells me my ModelState is invalid because the ResourceAssociation is null.

I can't specify the property in WithOne (e.g. WithOne(x => x.Product)) because ResourceAssociation is a table that will also be used for other tables (for example Brand also has a ResourceAssociation_Id column pointing to that table).

Is this kind of setup even possible in EF Core? Or will I have to add the different navigation properties to my ResourceAssociation class (ie Product, Brand,...)?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

In EF Core, HasOne / WithOne (even with navigation properties) are not enough for determining which is the principal and which is the dependent entity in the one-to-one relationship (for one-to-many the one is always principal, many is the dependent, and for many-to-many there is not principal/dependent).

Because of that, the generic type argument of HasForeignKey / HasPrincipalKey is required for determination of respectively dependent / principal entity. This is actually explained in the Other Relationship Patterns - One-to-one EF Core documentation topic:

When configuring the relationship with the Fluent API, you use the HasOne and WithOne methods.

When configuring the foreign key you need to specify the dependent entity type - notice the generic parameter provided to HasForeignKey in the listing below. In a one-to-many relationship it is clear that the entity with the reference navigation is the dependent and the one with the collection is the principal. But this is not so in a one-to-one relationship - hence the need to explicitly define it.

So what you need here is to specify Product as being the dependent of the relationship, along with the FK property / column name:

builder.HasOne(t => t.ResourceAssociation)
    .WithOne() // ok, no navigation
    .HasForeignKey<Product>("ResourceAssociation_Id") // FK name in the dependent
    //               ^^^ the dependent entity of the relationship
    .IsRequired(false)
    .OnDelete(DeleteBehavior.Cascade);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda