开发者

Entity Framework Code First Adding a null value for a id column

开发者 https://www.devze.com 2023-03-20 09:48 出处:网络
I am using Entity Framework code first and ASP.NET MVC 3. I have the following context defined: public class PbeContext : DbContext

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 Categ开发者_StackOverflow社区ory
{
    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.


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 --")
0

精彩评论

暂无评论...
验证码 换一张
取 消