Экземпляр типа сущности «Проект» не может быть отслежен
Вот соответствующий код для моей сущности Project:< /p>
Код: Выделить всё
public class Project
{
public Guid ProjectId { get; set; }
public string ProjectTitle { get; set; } = string.Empty;
public string ProjectDescription { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public string Category { get; set; } = string.Empty;
public string ImageFileName { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
public List AdditionalImageFileNames { get; set; } = new List();
// New field: If true, the project will be shown to the user
public bool IsFeatured { get; set; } = false;
}
сервиса:
Код: Выделить всё
private readonly IService _service;
public ProjectsController(IService service)
{
_service = service;
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Edit(Guid id, Project project, IFormFile? newImage, IEnumerable newOtherImages, string existingOtherImages)
{
if (ModelState.IsValid)
{
try
{
// Fetch all projects to perform checks
var allProjects = await _service.GetAllAsync();
var existingProject = allProjects.FirstOrDefault(p => p.ProjectId == id);
if (existingProject == null)
{
ModelState.AddModelError("", "Projeyi bulamadım!");
return View(project);
}
// Check if 'IsFeatured' is set to true and validate that only up to 3 projects can be featured
if (project.IsFeatured)
{
var featuredCount = allProjects.Count(p => p.IsFeatured);
if (featuredCount >= 3 && existingProject.IsFeatured == false)
{
ModelState.AddModelError("IsFeatured", "Sadece 3 proje öne çıkan olarak ayarlanabilir.");
return View(project);
}
}
// Handle the main image update
if (newImage != null)
{
project.ImageFileName = await FileHelper.FileLoaderAsync(newImage, "/Img/projectImages/");
}
else
{
project.ImageFileName = existingProject.ImageFileName; // Retain the existing image if none is uploaded
}
// Handle additional images (existing and new)
if (!string.IsNullOrEmpty(existingOtherImages))
{
project.AdditionalImageFileNames = existingOtherImages.Split(',').ToList();
}
else
{
project.AdditionalImageFileNames = existingProject.AdditionalImageFileNames; // Retain existing additional images
}
// Handle the new additional images upload
if (newOtherImages != null && newOtherImages.Any())
{
project.AdditionalImageFileNames.Clear();
foreach (var image in newOtherImages)
{
var fileName = await FileHelper.FileLoaderAsync(image, "/Img/projectImages/");
project.AdditionalImageFileNames.Add(fileName);
}
}
// Manually attach the modified project and set it to modified state
_service.Update(project);
await _service.SaveAsync(); // Save changes to the database
return RedirectToAction("Index");
}
catch (Exception ex)
{
// Log the exception details (you can also use a logging library for this)
ModelState.AddModelError("", $"Hata oluştu: {ex.Message}");
}
}
// Return the project back to the view if validation fails
return View(project);
}
Код: Выделить всё
public interface IRepository where T : class
{
List GetAll();
List GetAll(Expression expression);
T Get(Expression expression);
T Find(Guid id);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
int Save();
//Async Methods
Task GetAllAsync();
Task GetAllAsync(Expression expression);
Task SaveAsync();
Task FindAsync(Guid id);
Task GetAsync(Expression expression);
Task AddAsync(T entity);
}
Код: Выделить всё
public interface IService : IRepository where T : class, new()
{
object GetAsync();
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... or-when-up