【问题标题】:Entity Framework Complex Type Validation (DB First)实体框架复杂类型验证(数据库优先)
【发布时间】:2015-12-07 16:18:39
【问题描述】:

我目前正在使用 Entity framework 6 来连接用户界面和后端数据库,但是我在验证复杂类型的字段时遇到了问题。我使用的是数据库优先方法。

我基于现有数据库创建了模型,然后将字段转换为复杂类型。

例如,用户模型。

public partial class User {
    public User() {
        this.DeliveryAddress = new Address();
        this.InvoiceAddress = new Address();
    }

    public Address DeliveryAddress { get; set; }
    public Address InvoiceAddress { get; set; }
}

复合型

public partial class Address {
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string CompanyName { get; set; }
    public string Country { get; set; }
    public string County { get; set; }
    public string Firstname { get; set; }
    public string Postcode { get; set; }
    public string Surname { get; set; }
    public string TownCity { get; set; }
}

我在模型浏览器中正确映射了表格,一切都在编译。但是,在尝试保存时会引发验证失败的错误。经过一番调查,由于某种原因,所有复杂类型字段都是必需的。我觉得很奇怪,因为它们是可以为空的字符串,即使在数据库中,这些字段也可以为空。作为一种解决方法,我创建了一个带有构造函数的部分地址类,以将所有字段初始化为一个空字符串,这是一个临时解决方法。然而这并不理想。

保存细节的代码。 (Mvc 动作)

public ActionResult UpdateDetails(UserDetailsViewModel info) {
    try {
        if (ModelState.IsValid) {

            var user = db.Users.Find(info.UserID);
            if (user != null) {
                user.DeliveryAddress = new Data.Address {
                    CompanyName = info.DeliveryCompanyName,
                    Address1 = info.DeliveryAddress1,
                    Address2 = info.DeliveryAddress2,
                    Firstname = info.DeliveryFirstname,
                    Postcode = info.DeliveryPostcode,
                    Surname = info.DeliverySurname,
                    TownCity = info.DeliveryCity,
                };
                user.InvoiceAddress = new Data.Address();

                if (!info.IsInvoiceAddress) {
                    user.InvoiceAddress = new Data.Address {
                        Address1 = info.InvoiceAddress1,
                        Address2 = info.InvoiceAddress2,
                        Firstname = info.InvoiceFirstname,
                        Postcode = info.InvoicePostcode,
                        Surname = info.InvoiceSurname,
                        TownCity = info.InvoiceCity,
                        CompanyName = info.InvoiceCompanyName
                    };
                }

                db.SaveChanges();
            }

            return Json(new { Msg = "Ok" });
        }
    } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) {
        foreach (var valErs in dbEx.EntityValidationErrors) {
            foreach (var valEr in valErs.ValidationErrors) {
                System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", valEr.PropertyName, valEr.ErrorMessage);
            }
        }

        return Json(new { Msg = "Err" });
    } catch {
        return Json(new { Msg = "Err" });
    }

    return PartialView("UserDetailsForm", info);
}

有没有人遇到过类似的情况?也许我错过了一些需要配置的东西。

【问题讨论】:

  • user.InvoiceAddress = new Data.Address(); 可以删除我看不出这条线有什么用途。
  • @bilpor 该行正在帮助解决该问题,因为它将所有字段初始化为空字符串。复杂类型本身也不能为空,因此需要初始化。
  • 在这种情况下,您应该按照我所说的删除else 语句中的else。但是,复杂对象类型不能为空是不正确的。以 Entity Framework 为例,您可以将 LazyLoading 设置为 False,您的父对象可能有许多未加载的复杂类型。

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


【解决方案1】:

经过大量搜索,我找到了解决问题的方法。在复杂类型下的模型浏览器选项中,您可以展开复杂类型以显示复杂类型中的字段,并且在每个字段的属性中都有一个属性“Nullable”设置为 false。我将这些设置为 true,它现在可以毫无问题地保存。我很惊讶它没有选择类型并根据类型将其更改为允许空值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多