0

I am using Entity Framework code first and ASP.NET MVC 3.

I have the following context defined:

public class PbeContext : DbContext
{
     public DbSet<Category> Categories { get; set; }

     protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
     {
          dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
     }
}

My Category class:

public class Category
{
     public int Id { get; set; }
     public string Name { get; set; }
     public string Description { get; set; }
     public string MetaKeywords { get; set; }
     public string MetaDescription { get; set; }
     public bool IsActive { get; set; }
     public Category ParentCategory { get; set; }
     public int ParentCategoryId { get; set; }
}

ParentCategoryId in my Category table is a foreign key that is linked to the Id (Category Id) column.

On my create view there is a drop drown list displaying all the parent categories. You don't have to select a parent category. If none is selected then it means that you are creating/capturing a parent category.

How I normally did it in the past is if the user does not select a parent category from the drop down list then I would make ParentCategoryId in my Category table NULL. Currently it is trying to add 0 to the table and failing because there is no category with and Id of 0. Is it still best practices to add it as a NULL value if no parent category is selected? If so, how would I do it using Entity Framework code first?

EDIT

I have changed the int in my view model and Category class to nullable as suggested.

public class CreateCategoryViewModel
{
    public CreateCategoryViewModel()
    {
        IsActive = true;
    }

     public string Name { get; set; }
     public string Description { get; set; }
     public string MetaKeywords { get; set; }
     public string MetaDescription { get; set; }
     public bool IsActive { get; set; }
     public SelectList ParentCategoriesSelectList { get; set; }
     public int? ParentCategoryId { get; set; }
}

My updated category class:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string MetaKeywords { get; set; }
    public string MetaDescription { get; set; }
    public bool IsActive { get; set; }
    public int? ParentCategoryId { get; set; }
}

My dropdown list has a default "-- Select --" option with a value of 0. When I click submit the ParentCategoryId in the view model is 0. I map it to the category class and the ParentCategoryId is still zero. When and how is this converted to NULL? The error that I am still getting is:

The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_Category_Category". The conflict occurred in database "ProgrammingByExample", table "dbo.Category", column 'Id'.
The statement has been terminated.

1 Answer 1

1

Make ParentCategoryId nullable

public class Category
{
     public int Id { get; set; }
     public string Name { get; set; }
     public string Description { get; set; }
     public string MetaKeywords { get; set; }
     public string MetaDescription { get; set; }
     public bool IsActive { get; set; }
     public Category ParentCategory { get; set; }
     public int? ParentCategoryId { get; set; }
}

Edit

My dropdown list has a default "-- Select --" option with a value of 0

The default option should not have a value(you have put 0) Eg:

@Html.DropDownFor(model => model.ParentCategoryId, 
    (SelectList)ViewBag.ParentCategories, "-- Select --")
Sign up to request clarification or add additional context in comments.

3 Comments

Is this the best way to do it? Is it a wise choice to make an int nullable? Just want to hear your opinion.
@Brendan yes. that's how its usually done. (other method is to completely remove the scalar property ParentCategoryId and map it accordingly.)
@Brendan: It is actually the only way to do that. So yes it is wise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.