跟踪导航属性的更改

时间:2013-11-22 05:39:50

标签: entity-framework-4

 public partial class ClaimEntities : DbContext
        {
            public ClaimEntities()
                : base("name=ClaimEntities")
            {
                this.Configuration.LazyLoadingEnabled = false;
            }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }

            public DbSet<ClaimInformation> ClaimInformations { get; set; }
            public DbSet<ClaimInformation_HealthCareCode> ClaimInformation_HealthCareCode { get; set; }
           }

    }


    public partial class ClaimInformation 
    {
     public List<ClaimInformation_HealthCareCode> OtherDiagnosisCodes
            {
                get
                {
                    return this.ClaimInformation_HealthCareCode.Where(c => c.CodeQualifier == "BF" || c.CodeQualifier == "ABF").ToList();
                }

            }
    }


    public partial class ClaimInformation
        {
            public ClaimInformation()
            {
             this.ClaimInformation_HealthCareCode = new ObservableListSource<ClaimInformation_HealthCareCode>();
            }
public virtual ObservableListSource<ClaimInformation_HealthCareCode> ClaimInformation_HealthCareCode { get; set; }
        }

ClaimInformation_HealthcareCodes是实体ClaimInformation上的导航属性。 ClaimInformation与ClaimInformation_healthcareCodes之间的1-M。一个索赔可以有许多ClaimHealthcareCodes。 这是它在上下文中的加载方式

 _context.ClaimInformations.Include(h => h.ClaimInformation_HealthCareCode)

如何使上下文检测导航属性中的更改。在代码中我删除了healthcareCode条目。这是ClaimInformationClass中的列表,是虚拟的。 1-M this.claiminformation.claiminfo_healthcarecodes [I]卸下摆臂();此行连接到上下文。

     private string GetTableName(DbEntityEntry dbEntry)
            {
                string entryName = dbEntry.Entity.GetType().Name;
                int length = entryName.IndexOf('_');
                TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), 
false).SingleOrDefault() as TableAttribute;
                string tableName = tableAttr != null ? tableAttr.Name : entryName.Substring(0,length);
                return tableName;
            }

此代码将更改的条目作为'ClaimInformation'返回,这部分为真,但它必须更深入到具有已删除条目的navigation属性。

1 个答案:

答案 0 :(得分:0)

私有字符串GetTableName(DbEntityEntry ent)         {             ObjectContext objectContext =((IObjectContextAdapter)this.context).ObjectContext;             System.Type entityType = ent.Entity.GetType();

        if (entityType.BaseType != null && entityType.Namespace == "System.Data.Entity.DynamicProxies")
            entityType = entityType.BaseType;

        string entityTypeName = entityType.Name;

        EntityContainer container =
            objectContext.MetadataWorkspace.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace);
        string entitySetName = (from meta in container.BaseEntitySets
                                where meta.ElementType.Name == entityTypeName
                                select meta.Name).First();
        return entitySetName;
    }

将其更改为获取实体名称而不是表名