Hello everyone.
I use the Model First approach. There are two entities: Catalog and Book. The relationship between them is one-to-many. After creating the model, I automatically created classes for Catalog and Book:
namespace Library { using System; using System.Collections.Generic; public partial class Catalog { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Catalog() { this.Books = new HashSet<Book>(); } public System.Guid Id { get; set; } public string Name { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Book> Books { get; set; } } } namespace Library { using System; using System.Collections.Generic; public partial class Book { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Book() { this.Orders = new HashSet<Order>(); } public System.Guid Id { get; set; } public string Name { get; set; } public string Genre { get; set; } public string Author { get; set; } public string Year { get; set; } public virtual Catalog Catalog { get; set; } } } The context class looks like this:
namespace Library { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class LibraryModelContainer : DbContext { public LibraryModelContainer() : base("name=LibraryModelContainer") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Book> Books { get; set; } public virtual DbSet<Catalog> Catalogs { get; set; } } } Next, I want to add entries to the database tables for the Catalog and Book. I do this:
private void AddUpdateRecord_Click(object sender, RoutedEventArgs e) { using (LibraryModelContainer dbContainer = new LibraryModelContainer()) { Catalog catalog = new Catalog { Id = Guid.NewGuid(), Name = CatalogTextBox.Text }; dbContainer.Catalogs.Add(catalog); dbContainer.SaveChanges(); _window.CatalogDataGrid.ItemsSource = dbContainer.Catalogs.ToList(); } } private void AddUpdateRecord_Click(object sender, RoutedEventArgs e) { using (LibraryModelContainer dbContainer = new LibraryModelContainer()) { Book book = new Book { Id = Guid.NewGuid(), Name = BookAuthorTextBox.Text, Genre = BookGenreComboBox.Text, Author = BookAuthorTextBox.Text, Year = BookYearTextBox.Text, Catalog = (Catalog) BookCatalogComboBox.SelectedItem, }; dbContainer.Books.Add(book); dbContainer.SaveChanges(); _window.BookDataGrid.ItemsSource = dbContainer.Books.ToList(); } } When I add an entry to the Catalog table, I have no problems, since everything is clear how this is done. When I want to add a book to the Book table, here (dbContainer.SaveChanges ();) an exception occurs:
1) UpdateException: An error occurred while updating the records. For more information, see the inner exception. 2) SqlException: Violation of PRIMARY KEY constraint 'PK_Catalogs'. Cannot insert duplicate key in object 'dbo.Catalogs'. The duplicate key value is (aea5ebab-5453-449e-b181-7bee12ce7a3c). The statement has been terminated.
What am I specifically doing wrong when adding a record to the Book table?
Catalog = (Catalog) BookCatalogComboBox.SelectedItem,so it is impossible. You extract the selected directory, determine its Id, or other difference. sign, then refer to the database looking for a directory on the database, if you find, then then assign this found value to the desired book and then try to save it. Something like this. - Bulson