简体   繁体   中英

Entity Framework Code First: Computed property of type Type causes ArgumentNullException on Update-Database

As far as I understood, property of type Type cannot be directly stored in a database. Thus, I decided to create a wrapper - I want Entity Framework to store type name and create a property to return desired type:

[Table("DescriptiveCategory")]
public class DescriptiveCategory : Category {

    public string RelatedTypeName { get; set; }

    public Type RelatedType {
        get {
            return Type.GetType(RelatedTypeName);
        }
        set {
            RelatedTypeName = value.FullName;
        }
    }
}

This, unfortunately, does not work: if I try to update database, I get

Value cannot be null.
Parameter name: extent

If I comment RelatedType property, everything is fine. But I don't want Entity Framework to anything with this property - it serves only my needs. I could probably create get and set methods instead but I don't understand why this approach (with computed properties) doesn't work.

Use NotMappedAttribute :

Denotes that a property or class should be excluded from database mapping.

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