dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.79k stars 3.19k forks source link

API to set required dependents in the model when there is no navigation #30372

Open Liero opened 1 year ago

Liero commented 1 year ago

Question

I want to map multiple entities to the same table. I don't won't a navigation property as it would be artificial for my needs.

Why I'm getting this warning and can I get rid of it?

The entity type 'MaterialInfo2' is an optional dependent using table sharing without any required non shared property that could be used to identify whether the entity exists. If all nullable properties contain a null value in database then an object instance won't be created in the query. Add a required property to create instances with null values for other properties or mark the incoming navigation as required to always create an instance

See demo: https://dotnetfiddle.net/wt4BSQ

Code

public partial class Material 
{
    public string MaterialCode { get; set; }
    public string Popis { get; set; }
}

public class MaterialInfo2
{
    public string MaterialCode { get; set; } = string.Empty;
    public string Popis { get; set; }
}

public class MaterialConfiguration : IEntityTypeConfiguration<Material>
{
    public void Configure(EntityTypeBuilder<Material> entity)
    {
     entity.ToTable("Material");
        entity.HasKey(e => e.MaterialCode);
        entity.Property(e => e.MaterialCode)
            .HasColumnName("MaterialCode")
            .HasMaxLength(25)
            .IsRequired();
    }
}

public class MaterialInfo2Configuration : IEntityTypeConfiguration<MaterialInfo2>
{
    public void Configure(EntityTypeBuilder<MaterialInfo2> entity)
    {

        entity.ToTable("Material");
        entity.HasKey(e => e.MaterialCode);
        entity.Property(e => e.MaterialCode).
            HasColumnName("MaterialCode")
            .HasMaxLength(25)
            .IsRequired();

        entity.HasOne<Material>()
           .WithOne()
           .HasForeignKey<MaterialInfo2>(e => e.MaterialCode)
           .HasPrincipalKey<Material>(e => e.MaterialCode)
           .IsRequired(true);
    }
}

Provider and version information

EF Core version: 7.0.3 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 7.0

ajcvickers commented 1 year ago

Note for triage: the issue here is that there is no navigation that can be used to configure the dependent as required.

ajcvickers commented 1 year ago

@Liero There isn't currently a model building API to do this, but you can drop down to the underlying metadata and set IsRequiredDependent. For example:

entity.HasOne<Material>()
   .WithOne()
   .HasForeignKey<MaterialInfo2>(e => e.MaterialCode)
   .HasPrincipalKey<Material>(e => e.MaterialCode)
   .IsRequired(true)
   .Metadata.IsRequiredDependent = true;
Liero commented 1 year ago

worked for me. Should I close this issue, or you want to keep it open to track the missing api?

ajcvickers commented 1 year ago

Please leave it open.