实体框架代码首先为 id 列添加空值

发布于 2024-11-19 13:40:33 字数 2439 浏览 9 评论 0原文

我首先使用实体​​框架代码和 ASP.NET MVC 3。

我定义了以下上下文:

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

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

我的类别类:

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; }
}

我的 Category 表 中的 ParentCategoryId 是一个外键,它是链接到 Id(类别 ID)列。

在我的创建视图上有一个下拉列表,显示所有父类别。您不必选择父类别。如果未选择任何选项,则意味着您正在创建/捕获父类别。

我过去通常的做法是,如果用户没有从下拉列表中选择父类别,那么我会在我的 Category 表中创建 ParentCategoryId NULL。目前,它正在尝试将 0 添加到表中,但失败了,因为没有 ID 为 0 的类别。如果未选择父类别,将其添加为 NULL 值是否仍然是最佳实践?如果是这样,我将如何首先使用实体​​框架代码来做到这一点?

编辑

我已按照建议将视图模型和 Category 类中的 int 更改为可为空。

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; }
}

我更新的类别类:

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; }
}

我的下拉列表有一个默认的“--选择--”选项,值为0。当我单击提交时,视图模型中的ParentCategoryId为0。我将其映射到类别类,ParentCategoryId仍然是零。何时以及如何将其转换为 NULL?我仍然收到的错误是:

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-11-26 13:40:33

使 ParentCategoryId 可为空

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; }
}

编辑

我的下拉列表有一个默认的“-- Select --”选项,值为 0

默认选项不应有值(您已输入 0)
例如:

@Html.DropDownFor(model => model.ParentCategoryId, 
    (SelectList)ViewBag.ParentCategories, "-- Select --")

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 --")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文