When trying to add an element with a many-to-many relationship, a primary key violation error occurs:
I have two classes - Articles and Tags that are relevant to many to many:
public class Article { public int ID { get; set; } public string Text { get; set; } public ICollection<Tag> Tags { get; set; } } public class Tag { [Key] public string UrlSlug { get; set; } public string Name { get; set; } public ICollection<Article> Articles{ get; set; } }
When I add a new article, I allow the user to enter any tags, and then I want to create a new tag if the tag is not already created in the database, or add the tag to the tag collection of the Article object if the tag already exists. exists.
Therefore, when I create a new Article object, I call the following function:
public static Tag GetOrLoadTag(String tagStr) { string tagUrl = Tag.CreateTagUrl(tagStr); var db = new SnippetContext(); var tagFromDb = from tagdummy in db.Tags.Include(x => x.Articles) where tagdummy.UrlSlug == tagUrl select tagdummy; if (tagFromDb.FirstOrDefault() != null) { return tagFromDb.FirstOrDefault(); } else {
This function basically checks if there is an available tag in the database, and if so, returns that tag, which is then added to the tag collection of the Article object using article.Tags.Add ().
However, when I try to save this using the code below, I get a Violation of PRIMARY KEY error message
db.Entry(article).State = EntityState.Modified; db.SaveChanges();
I can't figure out how I should just create a relationship between an article and an existing tag.
Judo
source share