Efcore: EF Core 2 INSERT error (The property 'ID' on entity type 'xxx' has a temporary value)

Created on 24 Aug 2017  路  11Comments  路  Source: dotnet/efcore

This is my model (semplified):
PRAT is the main table

public partial class PRAT
{
    public int ID { get; set; }
    public string PRATICA { get; set; }
    public int ANNO { get; set; }
    public string VARIANTE { get; set; }

    [ForeignKey("ID")]
    public VW_PRATICHE_CONTIPO VW_PRATICHE_CONTIPO { get; set; }
}

VW_PRATICHE_CONTIPO is a View (not a table!) in the database that contains some data related to PRAT table (1 to 1 relationship)

public class VW_PRATICHE_CONTIPO
{
    public int ID { get; set; }
    public DateTime? DATAPRES { get; set; }
    public string PROTGEN { get; set; }
    public string TIPO { get; set; }
    public string TIPOEXTRA { get; set; }
    public string TIPOISTANZA { get; set; }
    public string TIPOPRAT { get; set; }
}

The one-to-one relation between the table and the View is based on the ID field.

I need this because I want to do a query like this:

context.PRAT.Include(x=> x.VW_PRATICHE_CONTIPO)

This query works as exptected.

The problem happens when I try to save a new entity in PRAT.

When i do this:

context.PRAT.Add(prat);
await context.SaveChangesAsync();

I got this error:

The property 'ID' on entity type 'PRAT' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.

If I remove the navigation property from PRAT all works fine, but I can't do the Include in my Query.

Can anybody help me?

Thank you.

Further technical details

EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: MS Windows 7
IDE: Visual Studio 2017

closed-fixed type-bug

Most helpful comment

@skysurfer72 The reason this is happening is that the foreign key is defined on the PRAT entity, which makes it the dependent end in the relationship. This means when saving changes, EF is expecting to get the FK value by propagation from the principal, but this doesn't happen because the principal does not exist. EF should at least throw a better exception in this case--leaving this issue open to triage that.

This can be made to work by defining the relationship the other way round so the FK is on the other type. For example:
C# modelBuilder .Entity<PRAT>() .HasOne(e => e.VW_PRATICHE_CONTIPO) .WithOne() .HasForeignKey<VW_PRATICHE_CONTIPO>();

All 11 comments

@skysurfer72 The reason this is happening is that the foreign key is defined on the PRAT entity, which makes it the dependent end in the relationship. This means when saving changes, EF is expecting to get the FK value by propagation from the principal, but this doesn't happen because the principal does not exist. EF should at least throw a better exception in this case--leaving this issue open to triage that.

This can be made to work by defining the relationship the other way round so the FK is on the other type. For example:
C# modelBuilder .Entity<PRAT>() .HasOne(e => e.VW_PRATICHE_CONTIPO) .WithOne() .HasForeignKey<VW_PRATICHE_CONTIPO>();

@skysurfer72 Thank you very much!

ajcvickers solution does not help in my case. and intellisense complains when you try to use .HasForeignKey on a type. Any more help for this out there?

I've got a many to many situation

           modelBuilder.Entity<ServiceAreaToZipCode>().HasKey(t => new { t.ServiceAreaId, t.ZipCodeId }); // composite key, watchout!

            modelBuilder.Entity<ServiceArea>()
                .HasMany(z => z.ServiceAreaToZipCodes)
                .WithOne(z => z.ServiceArea)
                .HasForeignKey(f => f.ServiceAreaId);

            modelBuilder.Entity<ZipCode>()
                .HasMany(pt => pt.ServiceAreaToZipCodes)
                .WithOne(t => t.ZipCode)                
                .HasForeignKey(pt => pt.ZipCodeId); 

"System.InvalidOperationException: The property 'ZipCodeId' on entity type 'ServiceAreaToZipCode' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property."

@redwards510 What version of EF Core are you using? There were some issues in this area in pre-2.0 releases.

Hi,

I have the same issue for Many To Many Relation.
I create a bug is npgsql git repo ( I can copy past the bug in this repo if needed ).

I'am using dotnet core 2.0.0.

@AlexTeixeira Can you add a link to the npgsql issue?

@ajcvickers Here the link https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/252

I was thinking first that come from the provider ( I test with SQL Server and it's works ). But right now is not working anymore...

@ajcvickers I am using EF Core 2.0.0, the latest with VS 2017.

I'm just trying to figure out how to specify which end of the relationship should be the dominant end. FWIW I've been using this article[[1]] as my reference for many-to-many tables.

I know that you can get around this error if you just manually create the column in question, but in this case I am using a object mapper called Mapster which kind of hides that from me.

@redwards510 Can you create a new issue with a complete code listing or project that reproduces what you are seeing?

@ajcvickers I feel like that's the only way I'm going to get past this blocker! I'll try. It's right in the middle of some complicated stuff though..

Fixed as part of #10142

Was this page helpful?
0 / 5 - 0 ratings