ASP.Net Core 2.2向数据库添加新的子实体也插入了一个新的空父实体

时间:2019-05-29 17:16:23

标签: c# sql-server asp.net-core asp.net-core-2.2

我最近将我的项目从ASP.Net Core 2.1更新到2.2(并遵循Microsoft的迁移指南),在更新了一些剃须刀页面以将子对象添加到数据库后,我也开始向该对象添加新的空父对象。同时存储数据库。奇怪的是,这种行为在所有子父母实体之间并不一致,并且某些剃刀页面仍然可以正常工作。

我挖了以下与我的问题有些相关的链接,但没有提供解决方案:

github issue确切描述了我似乎正在发生的事情,但是我没有使用自定义ModelBinder。

这是Create.cshtml:

<h2>Create</h2>

<h4>Branch</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Branch.BranchName" class="control-label"></label>
                <input asp-for="Branch.BranchName" class="form-control" />
                <span asp-validation-for="Branch.BranchName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.Name" class="control-label"></label>
                <input asp-for="Branch.Name" class="form-control" />
                <span asp-validation-for="Branch.Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.Address" class="control-label"></label>
                <input asp-for="Branch.Address" class="form-control" />
                <span asp-validation-for="Branch.Address" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.City" class="control-label"></label>
                <input asp-for="Branch.City" class="form-control" />
                <span asp-validation-for="Branch.City" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.Province" class="control-label"></label>
                <input asp-for="Branch.Province" class="form-control" />
                <span asp-validation-for="Branch.Province" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.Country" class="control-label"></label>
                <input asp-for="Branch.Country" class="form-control" />
                <span asp-validation-for="Branch.Country" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.PostalCode" class="control-label"></label>
                <input asp-for="Branch.PostalCode" class="form-control" />
                <span asp-validation-for="Branch.PostalCode" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.Status" class="control-label"></label>
                <input asp-for="Branch.Status" class="form-control" />
                <span asp-validation-for="Branch.Status" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.Phone" class="control-label"></label>
                <input asp-for="Branch.Phone" class="form-control" />
                <span asp-validation-for="Branch.Phone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.Fax" class="control-label"></label>
                <input asp-for="Branch.Fax" class="form-control" />
                <span asp-validation-for="Branch.Fax" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.Comments" class="control-label"></label>
                <textarea asp-for="Branch.Comments" class="form-control"></textarea>
                <span asp-validation-for="Branch.Comments" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Branch.DealerId" class="control-label"></label>
                <select asp-for="Branch.DealerId" class ="form-control" asp-items="ViewBag.DealerId"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

这是Create.cshtml.cs:

public class CreateModel : PageModel
    {
        private readonly ApplicationDbContext _context;

        public CreateModel(ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult OnGet()
        {
            Branch = new Branch();
            //Usually the DealerId would be passed in the URL, this is just for testing
            ViewData["DealerId"] = new SelectList(_context.Dealer, "DealerId", "DealerId");
            return Page();
        }

        //This is weird for debugging purposes
        private Branch _branch;

        [BindProperty]
        public Branch Branch {
            get
            {
                return _branch;
            }
            set
            {
                var t = value;
                _branch = value;
            }}

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Branches.Add(Branch);
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
    }

Branch.cs:

public class Branch
    {
        [Key]
        public string BranchId { get; set; }

        [Display(Name = "Branch Name")]
        public string BranchName { get; set; }

        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        public string Country { get; set; }

        [Display(Name = "Postal Code")]
        [DataType(DataType.PostalCode)]
        public string PostalCode { get; set; }


        public string Status { get; set; }

        [DataType(DataType.PhoneNumber)]
        public string Phone { get; set; }

        [DataType(DataType.PhoneNumber)]
        public string Fax { get; set; }

        [DataType(DataType.MultilineText)]
        public string Comments { get; set; }


        public string DealerId { get; set; }

        [ForeignKey("DealerId")]
        public Dealer Dealer { get; set; }

        public List<ApplicationUser> Users { get; set; }
    }

将页面发回时,Branch对象应具有null的Dealer属性(对于ASP.Net Core 2.1则应具有),但是具有一个新实例化的Dealer对象(具有空字段)here's a screenshot of the breakpoint on the Branch setter。将分支对象添加到数据库后,应使用DealerId查找相关对象并将其链接(在这些类/表之间建立了适当的功能性FK关系),但是因为Dealer导航属性不为null ,分支机构和空经销商都被添加到数据库,并在它们之间建立了FK关系。可以通过在将“分支”添加到数据库_context之前将Dealer属性设置为null来解决此问题,但这显然不是预期的行为,并且对于其他几个具有相似关系的数据库实体也不需要这样做。

我一直在试图弄清楚该错误的根源。我什至甚至创建了一个新的ASP.Net Core项目,复制了必要的模型,并从它们中搭建了一个新数据库,只是遇到了同样的问题。

据我所知,这是ModelBinder在这些类和其他相关类上表现不佳的问题。活页夹应在回发时将Dealer属性设置为null,但是由于某种原因,它正在调用默认构造函数(然后为Dealer的父类调用默认构造函数)。

1 个答案:

答案 0 :(得分:1)

经过大量搜索,我发现此行为是由与模型绑定对待具有IFormFile属性的实体的方式有关的问题引起的。

请参阅:

在我的情况下,Dealer类具有IFormFile属性,这导致模型绑定程序调用{​​{1}}的默认构造函数,导致填充了Dealer属性使用默认的Branch.Dealer对象,而不是保留为null。根据{{​​3}},此问题已在Dealer中解决。