0

I am trying to ignore a class property when inserting data to database using metadata for the class but it is not working. I am using using EF 6. I have tried both the metadata and partial class are in the same assembly as the classes generated by EF

[NotMapped] and [DatabaseGenerated(DatabaseGeneratedOption.Computed)]

Used a internal sealed class (for metadata) inside my partial class

namespace XeroDataStore.XeroDatabase
{
    [MetadataType(typeof(TempAddressMetadata))]
    public partial class TempAddress
    {
    }

    [MetadataType(typeof(TempContact.TempContactMetadata))]
    public partial class TempContact
    {
        internal sealed class TempContactMetadata
        {
            [NotMapped]
            public Nullable<System.DateTime> UploadDate { get; set; }
        }
    }
}

namespace XeroDataStore.XeroDatabase
{
    public class TempAddressMetadata
    {
        [NotMapped]
        public Nullable<System.DateTime> UploadDate { get; set; }
    }
}

EF Generated Class

namespace XeroDataStore.XeroDatabase
{
    public partial class TempAddress
    {
        public int RowId { get; set; }
        public int ClientID { get; set; }
        public System.Guid ContactID { get; set; }
        public string AddressType { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string AddressLine3 { get; set; }
        public string AddressLine4 { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public string AttentionTo { get; set; }
        public Nullable<System.DateTime> UploadDate { get; set; }

        public virtual TempContact TempContact { get; set; }
    }
}

What am I missing here?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Suman Palikhe
  • 357
  • 1
  • 4
  • 16

1 Answers1

0

Do it using Fluent API to make sure your model classes are POCO and have nothing to do with the data access.

In your data context, OnModelCreating methoed, use the following code to ignore the property

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TempContact>().Ignore(a => a.UploadDate );
        }
Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19