简体   繁体   中英

Unable to determine the relationship represented by navigation 'entitiy' of type 'ICollection<entity>'

I am aware of other questions on the same topic, however I have tried their solutions and none have worked for my code. I am configuring a one-to-many relationship with one Account with many JoinedClassIds.

Account.cs

public class Account {
        // Actual account info
        [Key]
        public int _id { get; set; }

        public string _name { get; set; }
        public string _email { get; set; }

        public ICollection<JoinedClassId> JoinedClasses { get; set; }
}

JoinedClassId.cs

[Keyless]
    public class JoinedClassId {
        public int classIdNumber { get; set; }
        public Account Account { get; set; }
    }

DBContext

        public DbSet<Account> Accounts { get; set; }
        public DbSet<JoinedClassId> JoinedClassIds { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder) {
            modelBuilder.Entity<Account>()
                .HasMany(acc => acc.JoinedClasses)
                .WithOne(jcid => jcid.Account);
            modelBuilder.Entity<JoinedClassId>()
                .HasOne(jc => jc.Account)
                .WithMany(acc => acc.JoinedClasses);
            modelBuilder.Entity<JoinedClassId>()
                .HasNoKey();
        }

I get the error

System.InvalidOperationException: 'Unable to determine the relationship represented by navigation 'Account.JoinedClasses' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'

What can I do to fix this?

Keyless entities cannot participate in two way relationships. They can only contain reference navigation to other regular entities (entities with key). Also, regular entities cannot contain navigation properties to keyless entity types. What causing the issue in your case is, your Account entity contains the navigation property JoinedClasses , which is a collection of Keyless entity.

For details - Keyless entity types characteristics

  1. Remove the [Keyless] attribute from JoinedClassId entity
  2. Add a new key property or mark the classIdNumber property as key
  3. Add a foreign-key property (optional)
public class JoinedClassId 
{
    [Key]
    public int Id { get; set; }    // added key property
    public int classIdNumber { get; set; }
    public int AccountId { get; set; }    // added foreign-key property
    
    public Account Account { get; set; }
}

Then you can configure as -

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Account>()
        .HasMany(acc => acc.JoinedClasses)
        .WithOne(jcid => jcid.Account)
        .HasForeignKey(p=> p.AccountId);  // if you have added a foreign-key property
}

Fix your classes:

public class Account {
       
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }
        public string Email { get; set; }

        public virtual ICollection<JoinedClass> JoinedClasses { get; set; }
}

    public class JoinedClass {
        [Key]
        public int Id { get; set; }
        public int ClassIdNumber { get; set; }

        public int AccountId { get; set; }
        public virtual Account Account { get; set; }
    }

If you use EF Net5, you don't need OnModelCreating fluent Api code. Delete all tables from db and migrate again

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM