ошибки с доб автора

This commit is contained in:
Екатерина Рогашова 2023-05-08 22:43:33 +04:00
parent aba1c6104e
commit cd675d40db
9 changed files with 79 additions and 54 deletions

View File

@ -18,6 +18,7 @@ namespace BookShopDataBaseImplement.Implements
{
using var context = new BookShopDatabase();
return context.Books
.Include(x => x.Genre)
.Include(x => x.Authors)
.ThenInclude(x => x.Author)
.ToList()
@ -96,6 +97,7 @@ namespace BookShopDataBaseImplement.Implements
{
using var context = new BookShopDatabase();
var element = context.Books
.Include(x => x.Genre)
.Include(x => x.Authors)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)

View File

@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace BookShopDataBaseImplement.Migrations
{
[DbContext(typeof(BookShopDatabase))]
[Migration("20230507194055_InitialCreate")]
[Migration("20230508181531_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -181,11 +181,13 @@ namespace BookShopDataBaseImplement.Migrations
modelBuilder.Entity("BookShopDataBaseImplement.Models.Book", b =>
{
b.HasOne("BookShopDataBaseImplement.Models.Genre", null)
b.HasOne("BookShopDataBaseImplement.Models.Genre", "Genre")
.WithMany("Books")
.HasForeignKey("GenreId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Genre");
});
modelBuilder.Entity("BookShopDataBaseImplement.Models.BookAuthor", b =>

View File

@ -178,11 +178,13 @@ namespace BookShopDataBaseImplement.Migrations
modelBuilder.Entity("BookShopDataBaseImplement.Models.Book", b =>
{
b.HasOne("BookShopDataBaseImplement.Models.Genre", null)
b.HasOne("BookShopDataBaseImplement.Models.Genre", "Genre")
.WithMany("Books")
.HasForeignKey("GenreId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Genre");
});
modelBuilder.Entity("BookShopDataBaseImplement.Models.BookAuthor", b =>

View File

@ -23,7 +23,7 @@ namespace BookShopDataBaseImplement.Models
public double Cost { get; set; }
[Required]
public int GenreId { get; set; }
public virtual Genre Genre { get; set; }
private Dictionary<int, IAuthorModel>? _bookAuthors = null;
[NotMapped]
public Dictionary<int, IAuthorModel> BookAuthors
@ -49,6 +49,7 @@ namespace BookShopDataBaseImplement.Models
BookName = model.BookName,
Cost = model.Cost,
Count = model.Count,
GenreId = model.GenreId,
Authors = model.BookAuthors.Select(x => new BookAuthor
{
Author = context.Authors.First(y => y.Id == x.Key)
@ -59,14 +60,27 @@ namespace BookShopDataBaseImplement.Models
{
BookName = model.BookName;
Cost = model.Cost;
Cost = model.Cost;
GenreId = model.GenreId;
}
public BookViewModel GetViewModel => new()
public BookViewModel GetViewModel
{
Id = Id,
get
{
using var context = new BookShopDatabase();
return new BookViewModel
{
Id = Id,
BookName = BookName,
Cost = Cost,
GenreId= GenreId,
GenreName = context.Genres.FirstOrDefault(x => x.Id == GenreId)?.GenreName ?? string.Empty,
BookAuthors = BookAuthors
};
};
}
}
public void UpdateAuthors(BookShopDatabase context, BookBindingModel model)
{
var bookAuthors = context.BookAuthors.Where(rec =>

View File

@ -1,6 +1,7 @@
using BookShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -14,6 +15,8 @@ namespace BookShopContracts.ViewModels
public double Cost { get; set; }
public int Count { get; set; }
public int GenreId { get; set; }
[DisplayName("GenreName")]
public string GenreName { get; set; } = string.Empty;
public Dictionary<int, IAuthorModel> BookAuthors { get; set; }
}
}

View File

@ -44,6 +44,8 @@
this.buttonSave = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.comboBoxGenre = new System.Windows.Forms.ComboBox();
this.ColumnID = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ColumnAuthorSurname = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.groupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.SuspendLayout();
@ -163,6 +165,9 @@
//
this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Control;
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.ColumnID,
this.ColumnAuthorSurname});
this.dataGridView.Location = new System.Drawing.Point(6, 26);
this.dataGridView.Name = "dataGridView";
this.dataGridView.RowHeadersWidth = 51;
@ -198,6 +203,21 @@
this.comboBoxGenre.Size = new System.Drawing.Size(261, 28);
this.comboBoxGenre.TabIndex = 10;
//
// ColumnID
//
this.ColumnID.HeaderText = "ID";
this.ColumnID.MinimumWidth = 6;
this.ColumnID.Name = "ColumnID";
this.ColumnID.Visible = false;
this.ColumnID.Width = 125;
//
// ColumnAuthorSurname
//
this.ColumnAuthorSurname.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.ColumnAuthorSurname.HeaderText = "Автор";
this.ColumnAuthorSurname.MinimumWidth = 6;
this.ColumnAuthorSurname.Name = "ColumnAuthorSurname";
//
// FormBook
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
@ -216,6 +236,7 @@
this.Controls.Add(this.labelName);
this.Name = "FormBook";
this.Text = "Книга";
this.Load += new System.EventHandler(this.FormBook_Load);
this.groupBox.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);
@ -241,5 +262,7 @@
private Button buttonSave;
private Button buttonCancel;
private ComboBox comboBoxGenre;
private DataGridViewTextBoxColumn ColumnID;
private DataGridViewTextBoxColumn ColumnAuthorSurname;
}
}

View File

@ -32,38 +32,26 @@ namespace BookShopView
_bookAuthors = new Dictionary<int, IAuthorModel>();
_logicG = logicG;
}
private readonly List<GenreViewModel>? _list;
public int GenreId
{
get
{
return Convert.ToInt32(comboBoxGenre.SelectedValue);
}
set
{
comboBoxGenre.SelectedValue = value;
}
}
public IGenreModel? GenreModel
{
get
{
if (_list == null)
{
return null;
}
foreach (var elem in _list)
{
if (elem.Id == GenreId)
{
return elem;
}
}
return null;
}
}
private void FormBook_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка жанров");
try
{
var list = _logicG.ReadList(null);
if (list != null)
{
comboBoxGenre.DisplayMember = "Genre";
comboBoxGenre.ValueMember = "ID";
comboBoxGenre.DataSource = list.Select(c => c.GenreName).ToList();
comboBoxGenre.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка жанров");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (_id.HasValue)
{
_logger.LogInformation("Загрузка книги");
@ -93,28 +81,13 @@ namespace BookShopView
}
private void LoadData()
{
_logger.LogInformation("Загрузка жанров");
try
{
var list = _logicG.ReadList(null);
if (list != null)
{
comboBoxGenre.DisplayMember = "GenreName";
comboBoxGenre.ValueMember = "ID";
comboBoxGenre.DataSource = list;
comboBoxGenre.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка жанров");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
_logger.LogInformation("Загрузка автора книги");
try
{
if (_bookAuthors != null)
{
dataGridView.Rows.Clear();
foreach (var pc in _bookAuthors)
{

View File

@ -57,4 +57,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="ColumnID.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnAuthorSurname.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root>