Witam,
Mam prostą bazę danych stworzoną z trzech tabel (relacje wiele do 1) w Entity Framework obsługiwaną za pomocą interfejsu webowego MVC 3.0. Do tabel zostały wygenerowane kontrolery CRUD.
Problem: usunięcie wymaganego elementu np. kategorii powoduje usunięcie wszystkich książek, które należały do tej kategorii. Rozumiem, że Framework zachowuje w ten sposób spójność bazy, ale jest to dla mnie wysoce niepożądane.
Pytanie: Jak najprościej wymusić by operacja usuwania została zaniechana, gdy inne tabele wymagają istnienia danego rekordu?
Myślałem, żeby obejść to tworząc zapytania LINQ. Wydaje mi się to jednak nieco skomplikowane i pewnie da się prościej, w końcu to raczej elementarna funkcjonalność. Jeśli nie da się inaczej będę wdzięczny za przykład albo naprowadzenie, gdzie szukać.
Moja baza danych:
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Firmowa.Models
{
public class Author
{
public int AuthorID { get; set; }
[Required(ErrorMessage = "Imię jest wymagane")]
[Display(Name="Imię")]
[MaxLength(45)]
public string Name { get; set; }
[Required(ErrorMessage = "Nazwisko jest wymagane")]
[Display(Name="Nazwisko")]
[MaxLength(45)]
public string Surname { get; set; }
}
}
<code class="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Firmowa.Models
{
public class Book
{
public int BookID { get; set; }
[Required(ErrorMessage = "Tytuł jest wymagany")]
[Display(Name = "Tytuł")]
public string Title { get; set; }
[Required(ErrorMessage = "ISBN jest wymagany")]
public string ISBN { get; set; }
[Required(ErrorMessage = "Autor jest wymagany")]
[Display(Name = "Autor")]
public int AuthorID { get; set; }
[Display(Name = "Rok")]
[Required(ErrorMessage = "Rok jest wymagany")]
public uint year { get; set; }
[Display(Name = "Wydawca")]
[Required(ErrorMessage = "Wydawca jest wymagany")]
public string Publisher { get; set; }
[Display(Name = "Kategoria")]
[Required(ErrorMessage = "Kategoria jest wymagana")]
public int CategoryID { get; set; }
public virtual Author Author { get; set; }
public virtual Category Category { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Firmowa.Models;
using Firmowa.Models.DAL;
namespace Firmowa.Models
{
public class BookContext : DbContext
{
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Book>().HasRequired(x => x.Author);
modelBuilder.Entity<Book>().HasRequired(x => x.Category);
}
}
}
<code class="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Firmowa.Models
{
public class Category
{
public int CategoryID { get; set; }
[Required(ErrorMessage = "Nazwa kategorii jest wymagana")]
[Display(Name = "Kategoria")]
public string Name { get; set; }
[Required(AllowEmptyStrings = true)]
[Display(Name="Opis")]
public string Description { get; set; }
}
}
Pozdrawiam,