Myridor
Myridor
CC#
Created by Myridor on 3/25/2024 in #help
✅ EF Core 8.0.3 Many To Many Relation
quick question, im using entity framework core(8.0.3) code first approach: I have 2 classes (many to many relation):
[PrimaryKey(nameof(Id), nameof(SecondId))]
public class A
{
public string Id { get; set; }
public string SecondId { get; set; }

public virtual ICollection<B> Bs;

public A()
{
Bs = new HashSet<B>();
}
}
[PrimaryKey(nameof(Id), nameof(SecondId))]
public class A
{
public string Id { get; set; }
public string SecondId { get; set; }

public virtual ICollection<B> Bs;

public A()
{
Bs = new HashSet<B>();
}
}
public class B
{
[Key]
public string Id { get; set; }

public virtual ICollection<A> As;

public B()
{
As = new HashSet<A>();
}
}
public class B
{
[Key]
public string Id { get; set; }

public virtual ICollection<A> As;

public B()
{
As = new HashSet<A>();
}
}
It's automatically creating the "third" table and on first entry its filling it aswell. Now the first time I add some new stuff it works fine. If I get the same Identifier tho it crashes, is there a way to automatically resolve it? or do I have to check if existing then add into table otherwise add to list?
b.As.Add(new A());
b.As.Add(new A());
Thanks in advance!
8 replies