简体   繁体   English

将项目插入到UnitofWork存储库-不保存对象-ASP.Net MVC

[英]Inserting items to UnitofWork Repository - objects not saving - ASP.Net MVC

I'm having a problem with the insertion code of a particular object or technically objects as I am looking to submit an image object and then a "KitchenItem" which is derived from a "Post" object that contains basic id, description strings. 我要提交图像对象,然后从包含基本ID,描述字符串的“ Post”对象派生的“ KitchenItem”时,遇到特定对象或技术对象的插入代码问题。

I have implemented the generic repository from the ASP.Net tutorials and this is working for other elements of the site. 我已经从ASP.Net教程中实现了通用存储库,并且该存储库可用于网站的其他元素。

Image Object: 图片对象:

public class Image
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Title { get; set; }

    public string AltText { get; set; }

    [DataType(DataType.Html)]
    public string Caption { get; set; }

    [Required]
    [DataType(DataType.ImageUrl)]
    public string ImageUrl { get; set; }

    private DateTime? createdDate { get; set; }
    [Required]
    [DataType(DataType.DateTime)]
    public DateTime CreatedDate
    {
        get { return createdDate ?? DateTime.UtcNow; }
        set { createdDate = value; }
    }
}

KitchenItem (I'm aware that I'm repeating code with the images, I want to attach particular images to elements within my site and the multiple uploads weren't achieving what I wanted to do... off topic though I'm open to suggestions for cleaner code) KitchenItem(我知道我要在图像上重复代码,我想将特定的图像附加到我网站中的元素上,并且多次上传都没有实现我想要的功能。对更干净的代码的建议)

  public class KitchenItem:Post
  {

    public Image MainSlider { get; set; }

    public Image MainSlider2 { get; set; }

    public Image MainSlider3 { get; set; }

    public Image MainSlider4 { get; set; }

    public Image MainSlider5 { get; set; }

    public Image SideImage1 { get; set; }

    public Image SideImage2 { get; set; }

  }

} }

View Model: 查看模型:

  public class CreateProjectViewModel
  {
    public KitchenItem  KitchenItem { get; set; }


    public HttpPostedFileBase MainSlider1 { get; set; }

    public HttpPostedFileBase MainSlider2 { get; set; }

     public HttpPostedFileBase MainSlider3 { get; set; }

     public HttpPostedFileBase MainSlider4 { get; set; }

     public HttpPostedFileBase MainSlider5 { get; set; }

     public HttpPostedFileBase SideImage { get; set; }

     public HttpPostedFileBase SideImage2 { get; set; }
}

Create function in controller: 在控制器中创建函数:

 public ActionResult Create( CreateProjectViewModel viewModel)
    {
        KitchenItem model = new KitchenItem();
        model = viewModel.KitchenItem;
        try
        {


                 // handle image uploads manually
                    if(viewModel.MainSlider1!=null){
                        model.MainSlider = AddImage(viewModel.MainSlider1, model.MainSlider);
                    }
                if(viewModel.MainSlider2!=null){
                    model.MainSlider2 = AddImage(viewModel.MainSlider2, model.MainSlider2);
                    }
                if( viewModel.MainSlider3!=null){
                    model.MainSlider3 = AddImage(viewModel.MainSlider3, model.MainSlider3);
                    }
                if(viewModel.MainSlider4!=null){
                    model.MainSlider4 = AddImage(viewModel.MainSlider4, model.MainSlider4);
                    }
                 if(viewModel.MainSlider5!=null){
                    model.MainSlider5 = AddImage(viewModel.MainSlider5, model.MainSlider5);
                    }
               if(viewModel.SideImage!=null){
                   model.SideImage1 = AddImage(viewModel.SideImage, model.SideImage1);
               }
                if(viewModel.SideImage2!=null)
                {
                    model.SideImage2 = AddImage(viewModel.SideImage2, model.SideImage2);
                }



                //Log date posted
                model.PostedOn = DateTime.Now;

                model.Category = unitofWork.CategoryRepository.GetByID(model.CategoryID);
                //create post
                unitofWork.KitchenItemRepository.Insert(model);
                unitofWork.Save();
                return RedirectToAction("Index");

        }
        catch (DataException dex )
        {
            //Log the error (uncomment dex variable name after DataException and add a line here to write a log.)
            ModelState.AddModelError("", "Unable to save changes.Image files not saved and Kitchen Item not Updated");
        }
        PopulateCategoriesDropDownList();
        return RedirectToAction("Index");

    }

Add image function: 添加图片功能:

 public Image AddImage(HttpPostedFileBase imageToSave, Image modelImage)
    {
        Image img = new Image();
        img = modelImage;
        img.ImageUrl=SaveUploadedFile(imageToSave, img.Id);
        unitofWork.ImagesRepository.Insert(img);
        return img;
    }

I've tracked the operation using break points and the AddImage function works and passing back the image successfully to Create() - Similarly the model within Create() shows that it contains all expected content right up until the unitofwork.kitchenItem.Insert(model); 我已经使用断点跟踪了该操作,并且AddImage函数起作用并将图像成功传递回Create()-类似地,Create()中的模型显示,该图像包含直到unitofwork.kitchenItem.Insert(model );

A couple of thoughts I have had are that it has something to do with the relational element of the functions, all of the image id's initiate as 0... although I would have assumed that the db insert would have handled this? 我有一些想法是,它与函数的关系元素有关,所有图像ID的初始值都为0 ...尽管我会假设db insert会处理这个问题?

Generic Repository: 通用存储库:

public class GenericRepository<TEntity> where TEntity : class
{
    internal GlenlithContext context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository(GlenlithContext context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

    public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

    public virtual TEntity GetByID(object id)
    {
        return dbSet.Find(id);
    }

    public virtual void Insert(TEntity entity)
    {
        dbSet.Add(entity);
    }

    public virtual void Delete(object id)
    {
        TEntity entityToDelete = dbSet.Find(id);
        Delete(entityToDelete);
    }

    public virtual void Delete(TEntity entityToDelete)
    {
        if (context.Entry(entityToDelete).State == EntityState.Detached)
        {
            dbSet.Attach(entityToDelete);
        }
        dbSet.Remove(entityToDelete);
    }

    public virtual void Update(TEntity entityToUpdate)
    {
        dbSet.Attach(entityToUpdate);
        context.Entry(entityToUpdate).State = EntityState.Modified;
    }
}

} }

Context: 语境:

public class Context : DbContext
{

    public Context()
        : base("Context")
    {
    }

    public DbSet<Category> Categories { get; set; }
    public DbSet<Tag> Tags { get; set; }
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<KitchenItem> KitchenItems { get; set; }
    public DbSet<Image> Images { get; set; }


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




        modelBuilder.Entity<Post>().HasOptional(c=>c.Category);
        modelBuilder.Entity<Post>().HasOptional(c => c.Tags);



        modelBuilder.Entity<KitchenItem>()
            .Map(m =>
          m.ToTable("KitchenItems"));

       modelBuilder.Entity<KitchenItem>().HasOptional(c => c.Category);
        modelBuilder.Entity<KitchenItem>().HasOptional(c => c.Tags);
        modelBuilder.Entity<KitchenItem>().HasMany(c => c.MainSliderPath);


    }
} 

View: 视图:

@model Glenlith.ViewModels.CreateProjectViewModel


@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_AdminLayout.cshtml";


 }
 <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js">        </script>
<style>
    .imagePreview {
        width: 870px;
       height: 420px;
      background-position: center center;
      background-size: cover;
     -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, .3);
     display: inline-block;
    }
    .sideImagePreview {
    width: 270px;
    height: 200px;
    background-position: center center;
    background-size: cover;
    -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, .3);
     display: inline-block;
 }

</style>
<div class=" row">
<h2>Create Kitchen Project</h2>
</div>


<div class=" row">
    @using (Html.BeginForm("Create", "KitchenItems", FormMethod.Post, new { EncType =     "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class=" row">
 <div class=" col-md-12">


    <div id="tabs">

        <ul>
            <li><a href="#tabs-1">Main Slider 1</a></li>
            <li><a href="#tabs-2">Main Slider 2</a></li>
            <li><a href="#tabs-3">Main Slider 3</a></li>
            <li><a href="#tabs-4">Main Slider 4</a></li>
            <li><a href="#tabs-5">Main Slider 5</a></li>
            <li><a href="#tabs-6">Side Image 1</a></li>
            <li><a href="#tabs-7">Side Image 2</a></li>
        </ul>

    <div id="tabs-1">
        <div class="form-group">
            @Html.LabelFor(model => model.KitchenItem.MainSlider.Title, htmlAttributes:  new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider.Title, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider.Title, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider.AltText, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider.AltText, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider.AltText, "", new { @class = "text-danger" })
                </div>
            </div>

            <div id="imagePreview" class=" imagePreview"></div>
            Images for the Main Slider should be saved as 870px by 420px
            <div class="form-group">
                @Html.LabelFor(model => model.MainSlider1, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.MainSlider1, new { type = "file", id = "uploadFile" })
                    @Html.ValidationMessageFor(model => model.MainSlider1,"", new { @class = "text-danger" })
                </div>
            </div>



        </div>


        <div id="tabs-2">
            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider2.Title, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider2.Title, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider2.Title, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider2.AltText, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider2.AltText, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider.AltText, "", new { @class = "text-danger" })
                </div>
            </div>

            <div id="imagePreview2" class=" imagePreview"></div>
            Images for the Main Slider should be saved as 870px by 420px
            <div class="form-group">
                @Html.LabelFor(model => model.MainSlider2, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.MainSlider2, new { type = "file", id = "uploadFile2" })
                    @Html.ValidationMessageFor(model => model.MainSlider2, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div id="tabs-3">
            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider3.Title, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider3.Title, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider3.Title, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider3.AltText, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider3.AltText, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider3.AltText, "", new { @class = "text-danger" })
                </div>
            </div>

            <div id="imagePreview3" class=" imagePreview"></div>
            Images for the Main Slider should be saved as 870px by 420px
            <div class="form-group">
                @Html.LabelFor(model => model.MainSlider3, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.MainSlider3, new { type = "file", id = "uploadFile3" })
                    @Html.ValidationMessageFor(model => model.MainSlider3, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div id="tabs-4">
            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider4.Title, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider4.Title, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider4.Title, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider4.AltText, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider4.AltText, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider4.AltText, "", new { @class = "text-danger" })
                </div>
            </div>
            <div id="imagePreview4" class=" imagePreview"></div>
            Images for the Main Slider should be saved as 870px by 420px
            <div class="form-group">
                @Html.LabelFor(model => model.MainSlider4, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.MainSlider4, new { type = "file", id = "uploadFile4" })
                    @Html.ValidationMessageFor(model => model.MainSlider4, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div id="tabs-5">
            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider5.Title, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider5.Title, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider5.Title, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.MainSlider5.AltText, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.MainSlider5.AltText, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.MainSlider5.AltText, "", new { @class = "text-danger" })
                </div>
            </div>
            <div id="imagePreview5" class=" imagePreview"></div>
            Images for the Main Slider should be saved as 870px by 420px
            <div class="form-group">
                @Html.LabelFor(model => model.MainSlider5, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.MainSlider5, new { type = "file", id = "uploadFile5" })
                    @Html.ValidationMessageFor(model => model.MainSlider5, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div id="tabs-6">
            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.SideImage1.Title, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.SideImage1.Title, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.SideImage1.Title, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.SideImage1.AltText, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.SideImage1.AltText, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.SideImage1.AltText, "", new { @class = "text-danger" })
                </div>
            </div>



            <div id="imagePreview6" class=" imagePreview"></div>
            Side Images should be saved as 270px by 200px
            <div class="form-group">
                @Html.LabelFor(model => model.SideImage, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.SideImage, new { type = "file", id = "uploadFile6" })
                    @Html.ValidationMessageFor(model => model.SideImage, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div id="tabs-7">
            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.SideImage2.Title, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.SideImage2.Title, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.SideImage2.Title, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.SideImage2.AltText, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.SideImage2.AltText, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.SideImage2.AltText, "", new { @class = "text-danger" })
                </div>
            </div>

            <div id="imagePreview7" class=" imagePreview"></div>
            Side Images should be saved as 270px by 200px
            <div class="form-group">
                @Html.LabelFor(model => model.SideImage2, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.SideImage2, new { type = "file", id = "uploadFile7" })
                    @Html.ValidationMessageFor(model => model.SideImage2, "", new { @class = "text-danger" })
                </div>
            </div>

        </div>
    </div>
</div>

        <div class=" col-md-4">

         <div class="form-group">
         @Html.LabelFor(model => model.KitchenItem.Title, htmlAttributes: new {    @class = "control-label col-md-4" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.Title, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.Title, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.ShortDescription, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.ShortDescription, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.ShortDescription, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class=" col-md-4">
                <div class="form-group">
                    @Html.LabelFor(model => model.KitchenItem.Description, htmlAttributes: new { @class = "control-label col-md-4" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.KitchenItem.Description, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.KitchenItem.Description, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.KitchenItem.Meta, htmlAttributes: new { @class = "control-label col-md-4" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.KitchenItem.Meta, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.KitchenItem.Meta, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.UrlSlug, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.KitchenItem.UrlSlug, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.KitchenItem.UrlSlug, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.KitchenItem.Published, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-10">
                    <div class="checkbox">
                        @Html.EditorFor(model => model.KitchenItem.Published)
                        @Html.ValidationMessageFor(model => model.KitchenItem.Published, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>


            <div class="editor-label">
                @Html.LabelFor(model => model.KitchenItem.CategoryID, "Category")
            </div>
            <div class="editor-field">
                @Html.DropDownList("CategoryID", String.Empty)
                @Html.ValidationMessageFor(model => model.KitchenItem.CategoryID)
            </div>




        </div>
        <div class=" row">
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>

    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</div>




<style type="text/css">
    .dz-max-files-reached {
        background-color: red;
    }
</style>
@section scripts{
<script type="text/javascript">

    //File Upload response from the server
    Dropzone.options.dropzoneForm = {
        maxFiles: 10,
        init: function () {
            this.on("maxfilesexceeded", function (data) {
                var res = eval('(' + data.xhr.responseText + ')');

            });
        }
    };


</script>

<script>
 $(function () {
    $("#tabs").tabs();
 });


     </script>

<script src="~/js/sliderUpload.js"></script>
 }

Any help would be appreciated I'm relatively new to MVC and am keen to build clean code and I know that getting my head around mapping and handling the database is the biggest hurdle for me. 任何帮助将不胜感激,因为我不是MVC的新手,并且渴望构建清晰的代码,而且我知道,尽我所能来映射和处理数据库是我最大的障碍。

    public Image AddImage(HttpPostedFileBase imageToSave, Image modelImage)
    {
        Image img = new Image();
        img = modelImage;
        img.ImageUrl=SaveUploadedFile(imageToSave, img.Id);
        unitofWork.ImagesRepository.Insert(img);

        unitOfWork.SaveChanges(); // you didn't save the change.

        return img;
    }

you didn't save the change, Unit Of Work will not persist the insert operation because of that. 您没有保存更改,因此,工作单元将不会保留插入操作。 check the code above. 检查上面的代码。 In unit of work you should have Save() or SaveChanges() method that wrap DbContext.SaveChanges() to persist your changes. 在工作单元中,您应该具有包装DbContext.SaveChanges()的Save()或SaveChanges()方法,以保留您的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 标准化存储库,UnitOfWork,IOC容器Asp.Net MVC - Standardize Repository, UnitOfWork, IOC Container Asp.Net MVC 在我的asp.net MVC Web应用程序中获取通用存储库+子存储库+ UnitOfWork - Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application ASP.NET MVC中的UnitOfWork错误 - Errors with UnitOfWork in asp.net mvc 在ASP.NET MVC 5中使用具有通用存储库模式UnitOfWork的继承接口的方法 - Using Method of Inherited Interface with Generic Repository Pattern, UnitOfWork in ASP.NET MVC 5 如何防止UnitOfWork将更改保存在ASP.NET Boilerplate中 - How to prevent UnitOfWork from saving changes in ASP.NET Boilerplate 在 MVC5 ASP.Net 应用程序中使用 Repository 和 UnitOfWork 模式时,如何修改数据库初始值设定项 Seed 方法? - How do modify database initializer Seed method when using Repository and UnitOfWork patterns in MVC5 ASP.Net application? ASP.NET MVC工作单元和业务服务/层 - ASP.NET MVC UnitOfWork and Business Services / Layer ASP.NET MVC 4在“编辑”视图中保存模型列表项 - ASP.NET MVC 4 Saving Model List Items in Edit View 插入关联表(ASP.NET MVC) - Inserting into association table (ASP.NET MVC) 嵌套的UnitOfWork在Asp.net应用程序中不起作用 - Nested UnitOfWork is not working in Asp.net application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM