none
Code-First: NotMappedAttribute ignored on private properties RRS feed

  • Question

  • Let's have following code:

    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Objects;
    
    namespace ConsoleApplication1 {
      class Program {
        static void Main(string[] args) {
          var c = new MyContext();
          var oc = c.ObjectContext;
        }
      }
    
    
      class MyContext : DbContext {
        static MyContext() { Database.SetInitializer<MyContext>(new DropCreateDatabaseIfModelChanges<MyContext>()); }
    
        public ObjectContext ObjectContext { get { return ((IObjectContextAdapter)this).ObjectContext; } }
    
        public DbSet<AEntity> EntitiesA { get { return Set<AEntity>(); } }
        public DbSet<BEntity> EntitiesB { get { return Set<BEntity>(); } }
      }
    
      class AEntity { public int Id { get; set; } }
    
      class BEntity {
        public int Id { get; set; }
        <span style="text-decoration:underline"><strong>[NotMapped] private AEntity As_private { get; set; }</strong></span>
      }
    }
    


    If I run the code EF creates table BEntities with two columns - Id (PK) and As_private_Id (FK to AEntities).

    I consider this a bug in EF 4.1 (reported on connect https://connect.microsoft.com/VisualStudio/feedback/details/675167)

    1. It should IMO ignore all private properties.
    2. It should definitely ignore properties decorated with NotMappedAttribute.

    This is especially big problem when my class explicitly implements interface properties of generic interfaces like

    Show IRelation<Show>.RelatedItem1 { get { return Show; } set { Show = value; } }
    

    because the property name is then considered invalid and my model does not work.

     

    Is that known bug? Is there any workaround? Can I turn discovery of such properties off by removing some Convention? I would be even happy with turning off discovery of all Navigation / FK properties.

    Any suggestions are appreciated. Thanks.

     

     


    Đ.
    Monday, June 13, 2011 1:24 PM

Answers

All replies

  • Hi Donny,

    Welcome!

    According to your description and code, I'd like to say it is a know issue, but there is a work around, you can refer here: http://social.msdn.microsoft.com/Forums/en-MY/adodotnetentityframework/thread/ea59808f-d4e4-4b6b-83f0-13972afadf16 , thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 14, 2011 7:11 AM
    Moderator
  • Hi Donny,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 17, 2011 3:41 AM
    Moderator
  • Hi Alan,

    sorry for late reply, but I didn't received notifications from forums till today.

    The problem with my code is that I cannot make the property public because it's explicit interface implementation. The interface requires use of explicit implementation because the class implements two interfaces with properties of same name but different type. Due to limitations of C# this neither can be made public nor I can implement interface member under different name.

    I think it'd be better to fix this issue in EF-CF rather than using horrible workarounds. But for time being I used workaround base on Mono.Cecil. After the assembly is compiled I run a tool (AssemblyPostrocessor - http://tools.codeplex.com/SourceControl/changeset/view/90543#1741355). It basically remaps interface implementation to a public property (which exists) and the it removes the explicit interface implementation (private) property completely from the assembly.

    Actually it'd be much better to write the model in VB - I'd have no problems implementing interface members using public properties of different names. But I have to use C# :-(.

    Anyway, I'll try the other workaround mentioned in http://social.msdn.microsoft.com/Forums/en-MY/adodotnetentityframework/thread/ea59808f-d4e4-4b6b-83f0-13972afadf16 using dynamically generated λ expression. I tried it before and it didn't work for me. Either I wrote the expression in a wrong way or it doesn't work when name of the property is "weird" like "IMyInterface<MyClass>.Item1".

     

    Thanks


    Đ.
    Thursday, June 30, 2011 8:23 AM