Hi i have a model class like
public class Individual : EntityBase
{
public Individual()
{
RelatedIndividuals = new HashSet<RelatedIndividual>();
}
public string FirstName { get; set; }
public string PersonalNumber { get; set; }
public ICollection<RelatedIndividual> RelatedIndividuals { get; set; }
}
This Individuals Will have some rrelated individuals With some relationType i want to have Many to many connections in same table so i created another class
public class RelatedIndividual
{
[Column("RelatedIndividualType")]
public RelatedIndividualType RelatedIndividualType { get; set; }
[Key, Column(Order = 0)]
public long IndividualId { get; set; }
[Key, Column(Order = 1)]
public long RelatedIndividualId { get; set; }
}
and i tried to do in my contextDB
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Individual>()
.HasMany(p => p.RelatedIndividuals)
.WithMany()
.Map(m =>
{
m.MapLeftKey("IndividualId");
m.MapRightKey("RelatedIndividualId");
m.ToTable("RelatedIndividual");
});
}
but i got error on WithMany
"Severity Code Description Project File Line Suppression State Error CS1061 'CollectionNavigationBuilder' does not contain a definition for 'WithMany' and no accessible extension method 'WithMany' accepting a first argument of type 'CollectionNavigationBuilder' could be found (are you missing a using directive or an assembly reference?) Repositories C:\Users\Luka\Desktop\Individuals\Repositories\Context\IndividualsDbContext.cs 23 Active "
any suggestions ? how to do it correctly ?
AFter Edit : I tried
public class Individual : EntityBase
{
public Individual()
{
RelatedIndividuals = new HashSet<RelatedIndividual>();
RelatedIndividualsOf = new HashSet<RelatedIndividual>();
}
[MaxLength(50),MinLength(2), Column(TypeName = "nvarchar(50)")]
public string FirstName { get; set; }
[MaxLength(50), MinLength(2), Column(TypeName = "nvarchar(50)")]
public string LastName { get; set; }
public ICollection<RelatedIndividual> RelatedIndividuals { get; set; }
public ICollection<RelatedIndividual> RelatedIndividualsOf { get; set; }
}
and my second class
public class RelatedIndividual
{
[Column("RelatedIndividualType")]
public RelatedIndividualType RelatedIndividualType { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long IndividualId { get; set; }
[ForeignKey("IndividualId")]
public Individual Individual { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long RelateIndividualId { get; set; }
[ForeignKey("RelateIndividualId")]
public Individual RelateIndividual { get; set; }
}
and my context looks like this
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RelatedIndividual>()
.HasKey(e => new { e.IndividualId, e.RelateIndividualId });
modelBuilder.Entity<RelatedIndividual>()
.HasOne(pt => pt.Individual)
.WithMany(p => p.RelatedIndividuals)
.HasForeignKey(pt => pt.IndividualId);
modelBuilder.Entity<RelatedIndividual>()
.HasOne(pt => pt.RelateIndividual)
.WithMany(t => t.RelatedIndividualsOf)
.HasForeignKey(pt => pt.RelateIndividualId)
.OnDelete(DeleteBehavior.Restrict);
}
when i create physical person just it works ok , when i create physical person and in physical person i add RelatedIndividual , Ids doesnt match and it adds in both RelatedIndividuals and relatedIndividualsOf for example if my individual which i created newly is Id 2 and i related him to Id 1 when it saves in base its 2 , 2 not 1 ,2 help please.