简体   繁体   English

实体框架向我的ASP.NET核心站点的数据库中插入长值而不是实际字符串值

[英]Entity Framework inserting long value instead of actual string value to database for my ASP.NET core site

My Entity Framework passes a string like this to the database "1b2ef80d-038a-49d8-973b-fc783a53b6a3" instead of the text i placed into the input field, which was "text". 我的实体框架将这样的字符串传递给数据库“ 1b2ef80d-038a-49d8-973b-fc783a53b6a3”,而不是将我放置在输入字段中的文本称为“文本”。 How can i only Insert the exact value into the table? 我怎样才能只将精确值插入表中?

The Database table i'm currently testing is just one column and is set to VARCHAR(400). 我当前正在测试的数据库表仅是一列,并设置为VARCHAR(400)。

Context class: 上下文类:

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

Model class: 型号类别:

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

View page: 查看页面:

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

And Controller: 和控制器:

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

i had to add the class, instead of just the context.Add i had to make it context.class.Add: 我必须添加类,而不仅仅是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);
        }
    }
}

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

相关问题 ASP.NET实体框架文本框到数据库 - Asp.Net Entity Framework textbox to database ASP.NET Core MVC &amp; C#:使用实体框架将数据插入数据库 - ASP.NET Core MVC & C# : insert data into database using Entity Framework Update-Database 命令在 ASP.Net Core / Entity Framework Core 中不起作用,因为数据库中的对象已经存在 - Update-Database command is not working in ASP.Net Core / Entity Framework Core because object in database already exists 如何在ASP.NET Core项目中设置实体框架 - How to setup Entity Framework in ASP.NET Core project ASP.NET实体框架标签显示数据库条目 - ASP.NET Entity Framework Label show Database entry 如何在没有实体框架的ASP.NET中使用Oracle数据库? - How to use a Oracle database in ASP.NET without Entity Framework? 如何使用 ASP.Net 实体框架搭建 SQL Server 数据库 - How to scaffold a SQL Server database with ASP.Net Entity Framework ASP.net实体框架检查数据库中是否存在 - ASP.net Entity Framework Check if exists in database 如何根据特定值查询数据库中的列 MVC ASP.NET Core - How to query a column in a database based on specific value MVC ASP.NET Core 使用ASP.NET插入数据库会导致错误 - Inserting into a database with ASP.NET causes error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM