Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure a non-shadow principal key for this relationship

I'm trying to build by database using Code first Entity framework, within a ASP Core 1, RC2 projects.

And I get this error message:

The key {'TempId'} contains properties in shadow state and is referenced by a relationship from 'TaskStatus' to 'TaskRunner.IsRunning'. Configure a non-shadow principal key for this relationship.

Any one have a idea on what this error means?

Note that I don't have ny column name tempId. I guest that EF7 build this column for itself.

Here is my entity

 public class TaskStatus
 {
        public Guid TaskConfigId { get; set; }
        public string LastResultValue { get; set; }
        public int RetryCount { get; set; }
        public TaskStatusEnum StatusEnum { get; set; }
        public DateTimeOffset LastUpdate { get; set; }
        public TaskResult TaskResult { get; set; }
        public TaskStatusEnum TaskStatusEnum { get; set; }
}

public class TaskRunner
{
    public DateTimeOffset RunAt { get; set; }
    public TaskConfig TaskConfig { get; set; }
    public Guid TaskConfigId { get; set; }
    public TaskStatus IsRunning { get; set; }
}
like image 336
Hugo Avatar asked Jun 02 '16 19:06

Hugo


People also ask

How do I add a foreign key in fluent API?

You can then configure foreign key properties by using the HasForeignKey method. This method takes a lambda expression that represents the property to be used as the foreign key.

What is principal key in EF core?

Principal key: The properties that uniquely identify the principal entity. This may be the primary key or an alternate key. Foreign key: The properties in the dependent entity that are used to store the principal key values for the related entity.

How do I create a composite primary key in Entity Framework Core?

The only way to configure composite keys is to use the HasKey method. You specify the properties that form the composite key by passing them in as properties of an anonymous type to the HasKey method.


1 Answers

I was able to fix this by manually specifying a property as the primary key on my models. You might try putting a [Key] attribute on TaskConfigId, and maybe adding a primary key field to TaskRunner.

This bug report alludes that using Navigation Properties requires explicit primary keys, rather than letting EF trying to decide which property is the PK based on naming conventions.

I believe Shadow Properties simply means the properties (table columns) that are created to support navigation properties, for example a column named IsRunningTaskStatusId on your TaskRunner table, which contains the primary key of the associated TaskStatus. https://docs.efproject.net/en/latest/modeling/shadow-properties.html

like image 134
Josh Armstrong Avatar answered Sep 28 '22 08:09

Josh Armstrong