简体   繁体   English

如何覆盖获取; 放; 字段的属性?

[英]How do I override the get; set; properties of a field?

I'm working with a database that due to reason out of my control - I cannot modify the schema.由于我无法控制的原因,我正在使用一个数据库 - 我无法修改架构。

This database has a field "CertificateId" that is non-nullable, however, the field is still considered to be optional.此数据库有一个不可为空的字段“CertificateId”,但是,该字段仍被认为是可选的。 When I load this field into my model (I'm using the DB first approach), it is of course tagged as being non-nullable, as you can see in the designer.cs当我将此字段加载到我的 model(我使用的是 DB 优先方法)中时,它当然被标记为不可为空,正如您在 Designer.cs 中看到的那样

[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = false)]
[DataMemberAttribute()]
public global::System.String CertificateId
{
    get
    {
        return _CertificateId;
    }
    set
    {
        OnCertificateIdChanging(value);
        ReportPropertyChanging("CertificateId");
        _CertificateId = StructuralObject.SetValidValue(value, false);
        ReportPropertyChanged("CertificateId");
        OnCertificateIdChanged();
    }
}

Since this field is optional, it gets passed null sometimes, which obviously fails validation.由于此字段是可选的,因此有时会通过 null,这显然无法通过验证。

Is there any way of overloading these properties that are autogenerated?有没有办法重载这些自动生成的属性? I would like to be able to check if the value passed into the set property is null, and if it is, set it to an empty string before it goes into validation.我希望能够检查传递给 set 属性的值是否为 null,如果是,则在验证之前将其设置为空字符串。

Or, is it possible to override the metadata for this property and have IsNullable set to true?或者,是否可以覆盖此属性的元数据并将 IsNullable 设置为 true?

First of all if the field is considered as not nullable you mustn't set its value to null.首先,如果该字段被认为不可为空,则不得将其值设置为 null。 Once you manually assign null in your code it's your bug.在您的代码中手动分配 null 后,这就是您的错误。

Another problem which have to be solved is default value (null) if you don't assign the value.如果您不分配值,则必须解决的另一个问题是默认值(null)。 This is well discussed in this question - I like the way with initializing the field with constructor.这在这个问题中得到了很好的讨论 - 我喜欢用构造函数初始化字段的方式。

If for any reason previous two options are not what you want you have a last choice of modifying the value in overriden SaveChanges .如果出于任何原因,前两个选项不是您想要的,您可以最后选择修改覆盖的SaveChanges中的值。 Something like:就像是:

public override int SaveChanges(SaveOptions options)
{
    var data = context.ObjectStateManager
                      .GetObjectStateEntries(EntityState.Added | EntityState.Modified)
                      .Where(e => !e.IsRelationship)
                      .Select(e => e.Entity)
                      .OfType(MyEntity);

    foreach(var entity in data)
    {
        if (entity.CertificateId == null)
        {
            entity.CertificateId = String.Empty;
        }
    }

    return base.SaveChanges(options);
}

If your field is optional, why not make the database column nullable, instead of putting in empty string?如果您的字段是可选的,为什么不让数据库列可以为空,而不是放入空字符串?

You could use poco's, or you could update the existing entity framework template file (T4 templates), so that you can add your null check within the template, and when the entity framework classes are generated is uses your template.您可以使用 poco,也可以更新现有的实体框架模板文件(T4 模板),以便您可以在模板中添加 null 检查,并在生成实体框架类时使用您的模板。 I'll try and find you some links I've used in the past.我会尝试为您找到一些我过去使用过的链接。

Here's information on using POCO's http://msdn.microsoft.com/en-us/library/dd456853.aspx这是有关使用 POCO 的http 的信息://msdn.microsoft.com/en-us/library/dd456853.aspx

Another way to handle this is to create a stored procedure, add that to your model, and handle your logic to make nulls empty strings within the sp.处理此问题的另一种方法是创建一个存储过程,将其添加到您的 model,并处理您的逻辑以使 sp 中的 null 为空字符串。

If You created your model classes with a designer, then you can manually set this property to be nullable in your model.如果您使用设计器创建了 model 类,则可以在 model 中手动将此属性设置为可为空。

Thanks guys, I simply used the constructor in a partial class to set my default:谢谢大家,我只是在部分 class 中使用构造函数来设置我的默认值:

public partial class Emp_Certificate
{
    //Constructor
    public Emp_Certificate()
    {
        this.CertificateID = "";
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM