实体框架更新特定字段

时间:2017-09-08 17:50:26

标签: c# asp.net-mvc entity-framework

我有一个这样的模型:

public class Company 
{
    public string Id { get; set; }
    public string Company { get;set; }
    public string Company_Sort { get; set; }
    public string Gender { get; set; }
}

控制器:

public bool addCompanyInfo(string userId, string companyName) 
{
    var record = dbContext.Company.Where(a => a.Id.Equals(userId)).SingleOrDefault();
    record.Company = companyName;
    record.Company_Sort = companyName.ToUpper();

    dbContext.SaveChanges();

    return true;
}

现在这已经习惯了我以前更新了这样的记录,没有问题。但现在每次点击SaveChanges(),我都会收到验证错误,说明需要性别我甚至将性别视为我视图中的隐藏输入。

我甚至尝试过这样的事情,但无济于事:

dbContext.Company.Attach(record);

var entry = dbContext.Entry(record);
entry.Property(a => a.Company).IsModified = true;
entry.Property(a => a.Company_Sort).IsModified = true;

dbContext.SaveChanges();

该模型基于SQL Server中的一个表,其中Gender是一个不可为空的列。然后在配置类中映射和配置:

public partial class CompanyConfig : EntityTypeConfiguration<Company> 
{
    public CompanyConfig() : this("dbo") {}

    public CompanyConfig(string schema) 
    {
        ToTable(schema + ".Company");
        HasKey(x => x.Id);
        Property(x => x.Id).HasColumnName("Id").IsRequired().......
        Property(x => x.Gender).HasColumnName("Gender").IsRequired().....
    }
}

Razor查看:

<div class="dialog-content container-fluid">
        <div class="form-horizontal panel-body">
            @Html.HiddenFor(m => m.Id)
            @Html.HiddenFor(m => m.UserId)
            @Html.HiddenFor(m => m.SelectedKeywordText, new {@id = "txtKeyword"})
            @Html.HiddenFor(m => m.Address)
            @Html.HiddenFor(m => m.Designation)
            @Html.HiddenFor(m => m.FirstName)
            @Html.HiddenFor(m => m.MiddleName)
            @Html.HiddenFor(m => m.LastName)
            @Html.HiddenFor(m => m.Prefix)
            @Html.HiddenFor(m => m.CompanyName)
            @Html.HiddenFor(m => m.FullName)
            @Html.HiddenFor(m => m.Gender)
            @Html.HiddenFor(m => m.LastFirstName)
            @Html.HiddenFor(m => m.Email)

            <h5><b>Select a Keyword:</b></h5><br/>
            <div class="form-group">
                @Html.ValidationSummary()
                @Html.LabelFor(m => m.SelectedKeywordId, $"{Html.DisplayNameFor(m => m.SelectedKeywordId)}:", new {@class = "col-sm-3 control-label"})
                <div class="col-sm-6">
                    @Html.DropDownListFor(m => m.SelectedKeywordId, new SelectList(Model.KeyWords, "Id", "Keyword"), "Select a keyword", new {@class = "form-control", id = "ddlKeyword"})
                </div>
            </div>
        </div>
    </div>
    <nav class="navbar navbar-default dialog-navbar" role="navigation">
        <div class="container">
            <button type="button" id="btnSubmit" class="btn btn-primary navbar-btn">Send Email</button>
            <a id="btnCancelGrant" class="btn btn-link navbar-btn" href="#">Cancel</a>
        </div>
    </nav>
}
</div>

该视图使用的ViewModel与公司模型基本相同,只是它中包含更多字段。

为什么我尝试的两个选项不起作用?

0 个答案:

没有答案