I'm really new to ASP.NET MVC & Entity Framework and it's giving me so many headaches. I've gone through all of the similar questions and I've come up empty. I cannot find any good resources on ASP.NET MVC Error Handling!
Here are my models:
namespace ServerManager.Models
{
public class Domain
{
public Domain()
{
this.Id = Guid.NewGuid();
this.Servers = new HashSet<Server>();
}
[Key]
[Required]
public Guid Id { get; set; }
[Required]
[Display(Name="Domain Name")]
public string Name { get; set; }
[Required]
[Display(Name="Fully-Qualified Name")]
public string FullyQualifiedName { get; set; }
[Display(Name="Servers")]
public virtual ICollection<Server> Servers { get; set; }
}
public class Server
{
public Server()
{
this.Id = Guid.NewGuid();
}
[Required]
[StringLength(25)]
[Display(Name="Server Name")]
public string Name { get; set; }
public virtual Domain Domain { get; set; }
[Required]
[Display(Name = "Domain")]
public virtual Guid DomainId { get; set; }
}
}
I'm looking to be able to access the Server.Domain
property to lazily access the Domain
for the Server
object I'm working with (which works) and conversely, I'm looking to access the Domain.Servers
property (which kind of works). The problem is that when I delete a Domain
object using the DbContext
, a cascading delete takes place. The desired effect that I'm going for is to not allow the user to delete a Domain
object while there are still Server
objects using the Domain
. How can I achieve this without displaying a generic Error message?