Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark Entity Framework class properties as 'Obsolete'

I have some fields in Entity Framework entities which I would like to mark as [Obsolete]. Is it possible?

I have the following auto-created code:

namespace Data.Databases
{
    using System;
    using System.Collections.Generic;

    public partial class Address
    {
        public long Id { get; set; }
        public string CompanyName { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string HouseNumber { get; set; }
    }
}

I tried adding metadata like this:

namespace Data.Databases 
{
    [MetadataType(typeof(AddressMetadata))]
    public partial class Address 
    {
    }
    public class AddressMetadata 
    {
        [Obsolete]
        public string Title { get; set; }
    }
}

But that doesn't work. It compiles ok, but it doesn't show the field as obsolete in VS.

Is it something I am doing wrong or is it just not possible?

(I am using EF6 with C#4.5 in VS2012.)

like image 518
Ulric Avatar asked Dec 14 '15 14:12

Ulric


1 Answers

You could add an interface that has the member marked as Obsolete.

namespace Data.Databases 
{
    [MetadataType(typeof(AddressMetadata))]
    public partial class Address : IAddress
    {
    }
    public class AddressMetadata 
    {
        public string Title { get; set; }
    }

    public interface IAddress
    {
        [Obsolete]
        public string Title { get; set; }
    }
}
like image 150
Daniel A. White Avatar answered Sep 30 '22 19:09

Daniel A. White