简体   繁体   中英

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

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

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.

Or, is it possible to override the metadata for this property and have IsNullable set to true?

First of all if the field is considered as not nullable you mustn't set its value to null. Once you manually assign null in your code it's your bug.

Another problem which have to be solved is default value (null) if you don't assign the value. 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 . 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. 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

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.

If You created your model classes with a designer, then you can manually set this property to be nullable in your model.

Thanks guys, I simply used the constructor in a partial class to set my default:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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