开发者

How to use EF code first models to use relationships in order to generate data

开发者 https://www.devze.com 2023-03-04 13:41 出处:网络
I have two domain models in my project: Category and Sub-category. This is my one to many relationship and how I created it:

I have two domain models in my project: Category and Sub-category. This is my one to many relationship and how I created it:

public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }

    public List<SubCategory> SubCategories { get; set; }
}

public class SubCategory
{
    public int SubCategoryId { get; set; }
    public string SubCategoryName { get; set; }

    public Category Category { get; set; }
}

Now I am not sure how to add a Sub-Category to a Category, but I want 开发者_如何学Cthe association to be kept for each Sub-Categories in a Category. So I tried passing a Category to my CreateSubCategory View

public ActionResult CreateSubCategory(int categoryId)
{
    return View(_service.GetCategory(categoryId));
}

I came to here in my view and got stumped

@using (Html.BeginForm()){
    <h2>SubCategory</h2>

    <div>@Html.DisplayFor(model => model.CategoryName)</div>

    <div>@*Want to create Sub-category here*@</div>

    <p><input type="submit" value="Create" /></p>
}

I guess this is a noob question but I cannot figure out what to do here. So is there any advice on how to do this, or any way of performing this task of adding my Sub-categories to Categories?


First of all, add the virtual keyword.

public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }

    public virtual ICollection<SubCategory> SubCategories { get; set; }
}

// add constructor here for category, and set SubCatogories to new HashSet();

public class SubCategory
{
    public int SubCategoryId { get; set; }
    public string SubCategoryName { get; set; }
    public int CategoryId { get; set; }// add this:)

    public virtual Category Category { get; set; }
}

now there are two ways to add a subcategory; either get an instance of a Category and then add an instance of SubCategory to its SubCategories collection, or create a new SubCategory and set its fields including the CategoryId and then add it to its corresponding DbSet.


If you're creating a SubCategory, you should be passing a SubCategory (or SubCategoryViewModel) object as the model to your View.

You can create a new instance of the SubCategory in your CreateSubCategory() method and assign the CategoryId to the new object (as hazimdikenli pointed out). Be sure to store the Id using @Html.HiddenFor(m => m.CategoryId) so that it's persisted for the returning Post method.

public ActionResult CreateSubCategory(int categoryId)
{
    SubCategory model = new SubCategory();
    model.CategoryId = categoryId;
    return View(model);
}

When the object is posted back, that is the point where you commit to the database, saving the new object and creating the association with the parent Category object (via the Id reference).

If you want to pass the CategoryName, you should probably create a view model with a CategoryName string property (this would of course need to be populated in the CreateSubCategory() method)


It may be simpler to use a single domain model, especially if subcategories can have their own subcategories:

public class Category
{
    public int CategoryId { get; set; }

    public string Name { get; set; }

    public virtual Category ParentCategory { get; set; }

    public virtual ICollection<Category> ChildCategories { get; set; }
}

Then configure the relationship in the OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    ...

    modelBuilder.Entity<Category>()
        .HasMany(x => x.ChildCategories)
        .WithOptional(x => x.ParentCategory);

}

To create a new subcategory simply create a new Category and set its ParentCategory before sending the subcategory to your view.

0

精彩评论

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