''propertyName' cannot be used as a property on entity type 'typeName' because it is configured as a navigation
I have an entity user with the following:
public class User
{
[Key, Required]
public int Id { get; set; }
public int GenderId { get; set; }
public virtual Gender Gender { get; set; }
}
In gender:
public class Gender
{
[Key, Required]
public int Id { get; set; }
public virtual ICollection<User> Users { get; set; }
}
Then, inside my DbContext, I have:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>(user =>
{
user
.HasOne(x => x.Gender)
.WithMany(x => x.Users)
.HasForeignKey(x => x.GenderId);
}
user.HasIndex(x => x.Gender);
}
When I run dotnet ef add migration User, I am getting the error:
'Gender' cannot be used as a property on entity type 'User' because it is configured as a navigation.
Solution 1:[1]
I was trying to create an index on a navigation property. Instead, create the index on the foreign key.
Change user.HasIndex(x => x.Gender) to user.HasIndex(x => x.GenderId).
Solution 2:[2]
Use [ForeignKey("GenderId")] on your public virtual Gender Gender { get; set; } property . Thus GenderId would be identified as a foreign key for Associated User.
See below updated code:
public class User
{
public int GenderId { get; set; }
[ForeignKey("Id")]//Gender Primary key
public virtual Gender Gender { get; set; }
}
Hope it will fix your issue.
Thanks,
Solution 3:[3]
I had a similar error:
'Product' cannot be used as a property on entity type 'OrderLine' because it is configured as a navigation.
The cause of the error was that in the fluent api I also used the entity as a property:
modelBuilder.Entity<OrderLine>().Property(ol => ol.Product).IsRequired(true)
Solution 4:[4]
I had the same problem for a complex hierarchy model with nested complex classes in it.
Apparently, the IsRequired() method conflicts with OnDelete(...). I removed the IsRequired() and everything got back to normal.
public class MyComplexClassConfig : IEntityTypeConfiguration<MyComplexClass>
{
public void Configure(EntityTypeBuilder<MyComplexClass> builder)
{
builder
.HasOne(col => col.ChildClass)
.WithOne(col => col.ParentClass)
.OnDelete(DeleteBehavior.Cascade);
// The following line of code needs to be deleted.
builder.Property(col => col.Customer).IsRequired();
}
}
Solution 5:[5]
An update due to a similar problem:
I had same problem when I was stablishing a relation between Product Entity and Warranti Entity as below:
public class Product
{
public long Id { get; set; }
public string Images { get; set; }
public string Brand { get; set; }
public Warranti Warranti { get; set; }
}
public class Warranti
{
public Product Product { get; set; }
public long ProductId { get; set; }
public int WarrantyPeriod { get; set; }
}
and on my DatabaseContext Class I had this config:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasOne(p => p.Warranti)
.WithOne(p => p.Product)
.HasForeignKey<Warranti>(p => p.Product);
}
And I got Same error. But, What was The problem?
According to Microsoft Official EFCore Doc The ForeignKey Should be something like this:
.HasForeignKey(p => p.BlogForeignKey); // single prop such as string or int,...
Or
.HasForeignKey("BlogForeignKey"); // Hardcoded favorite string
But I was Using a class(Product) instead of a property In HasForeignKey method. likewise, you were using a class(Gender) instead of a property In HasIndex method. Changing these classes to an Id property solves the problem.
As a conclusion: Try to use an Id property on HasForeignKey() and HasIndex();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nimish David Mathew |
| Solution 2 | |
| Solution 3 | Mill |
| Solution 4 | Rzassar |
| Solution 5 | Mohammad Taheri |
