【问题标题】:Entity Framework inserting long value instead of actual string value to database for my ASP.NET core site实体框架将长值而不是实际字符串值插入到我的 ASP.NET 核心站点的数据库中
【发布时间】:2017-04-25 17:38:44
【问题描述】:

我的实体框架将这样的字符串传递给数据库“1b2ef80d-038a-49d8-973b-fc783a53b6a3”,而不是我放置在输入字段中的文本,即“文本”。我怎样才能只将确切的值插入表中?

我当前正在测试的数据库表只有一列,并且设置为 VARCHAR(400)。

上下文类:

modelBuilder.Entity<Contract>(entity =>
            {
                entity.HasKey(e => e.Contract1)
                    .HasName("Contract$PrimaryKey");

                entity.Property<string>(e => e.Contract1)
                    .HasColumnName("Contract")
                    .HasColumnType("VARCHAR(400)")
                    .IsUnicode(false)
                    .HasMaxLength(400);
            });

模型类:

     [Key]
            [Column(TypeName = "VARCHAR(400)")]
            [StringLength(400)]
            public string Contract1 { get; set; }

查看页面:

<form asp-action="Create">
    <div class="form-horizontal">
        <h4>Contract</h4>
        <hr />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            @Html.LabelFor(model => model.Contract1, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
            @Html.EditorFor(model => model.Contract1)
            @Html.ValidationMessageFor(model => model.Contract1)
                <span class="text-danger"></span>
            </div>
        </div>
        <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>
</form>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
</body>
</html>

和控制器:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Contract")] Contract Contract)
        {
            if (ModelState.IsValid)
            {
                _context.Add(Contract);
                await _context.SaveChangesAsync();
                return RedirectToAction("Create");
            }
            ViewData["Contract"] = new SelectList(_context.Contract, "Contract", "Contract", Contract.Contract1);
            return View(Contract);
        }
    }
}

【问题讨论】:

    标签: sql asp.net entity-framework ef-fluent-api


    【解决方案1】:

    我必须添加类,而不仅仅是 context.Add 我必须将其设为 context.class.Add:

     [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<ActionResult> Create(Contract Contract)
            {
                if (ModelState.IsValid)
                {
                    _context.Contract.Add(Contract);
                    await _context.SaveChangesAsync();
                    return RedirectToAction("Index");
                }
                ViewData["Contract"] = new SelectList(_context.Contract, "Contract", "Contract", Contract.Contract1);
                return View(Contract);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2018-12-17
      相关资源
      最近更新 更多