none
How to gain access to entity mappings at runtime? RRS feed

  • Question

  • I'm trying to add a change log to my application by overriding SaveChanges and logging the changes, I want to obtain and log the table and field names that are being updated rather than the entity class.
    For example (although a bad example) , MyEntity class might map to MyEntityDbTable, and the ID field might map to the MyEntityDbTableID field. 
    	
    this.ToTable("MyEntityDbTable");
    
    this.HasKey(t => t.ID);
    
    this.Property(t => t.ID)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    this.Property(t => t.ID).HasColumnName("MyEntityDbTableID");
    
    this.Property(t => t.Name)
    	.IsRequired()
    	.HasMaxLength(20);
    this.Property(t => t.Name).HasColumnName("MyNameField");
    
    

    When within SavingChanges I know MyEntity has changed and the Name property has changed, but I would like to log the update against MyEntityDbTable and MyNameField.
    How do I find this mapping at runtime?  I've tried accessing the MetadataWorkspace, as follows, but the GetItems list returned contains the details regarding the entity class and their properties and no mapping information to the database or tables, i.e. MyEntity maps to MyEntityDbTable and Name property maps to MyNameField etc...
     ObjectContext objContext = ((IObjectContextAdapter)this).ObjectContext;
     var mdw = objContext.MetadataWorkspace;
    
     var items = mdw.GetItems < EntityType > (DataSpace.CSpace);
    
    

    I can't seem to find anything that allows you to re-query the entity and database mappings once the data context has been created.  Forgot to mention that I'm using the Code First approach.
    Andrew
     
     
     
     
     
     
     
     
     
    Friday, January 6, 2012 2:46 PM

All replies