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 do.net 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.
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)
.
Use [ForeignKey("GenderId")]
on your public virtual Gender Gender { get; set; }
public virtual Gender Gender { get; set; }
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,
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)
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();
}
}
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() ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.