Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 590 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'propertyName' cannot be used as a property on entity type 'typeName' because it is configured as a navigation

#1
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.

Reply

#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,
Reply

#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)
Reply

#4
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)`.
Reply

#5
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();
}
}
Reply

#6
**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][1] 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()**;





[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through