简体   繁体   中英

Entity Framework 6 How to Map a UserDefined datatype property as varchar type column in DB

I have to map a Userdefined type Property as a Varchar Column Using EntityFramework 6. So I have an entity called EngineQuota :

'''

 [Table(EngineQuotas)]
    public partial class EngineQuota
    {
        public int Id { get; set; }
        
        public DecimalList Quotas { get; set; }

        public int? EngineId { get; set; }    

        private EngineQuotas()
        {
            this.Quotas = new DecimalList();
        }
    }

'''

The DecimalList is a seperate class which holds a sortedList of decimals '''

public class DecimalList : IEnumerable<decimal>
    {
        private readonly SortedSet<decimal> items;

        public DecimalList()
        {
            this.items = new SortedSet<decimal>();
        }

        public IEnumerator<decimal> GetEnumerator()
        {
            return this.items.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

       
    }
}

''' In DB I want a comma seperated value of items in DecimalList to be stored in a Column Quota in Table EngineQuotas. How is this possible in Entity Framework6 . Tried Using ComplexType but not able to get some result

Take a look at EF custom converters - this should solve it.
One thing - if you are converting collection, than it's possible, that You will need to provide custom comparer in this entity's configuration, because it is possible, that change tracker wont detect collection update.

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