Programmatically obtain Foreign keys between POCOs in Entity Framework 6

不羁的心 提交于 2019-12-01 23:22:39

To get the FK property's names for an specific entity you can use this generic method:

public IEnumerable<string> GetFKPropertyNames<TEntity>() where TEntity:class
{
        using (var context = new YourContext())
        {
            ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
            ObjectSet<TEntity> set = objectContext.CreateObjectSet<TEntity>();
            var Fks = set.EntitySet.ElementType.NavigationProperties.SelectMany(n=>n.GetDependentProperties());
            return Fks.Select(fk => fk.Name);
        }
 }

And if you want the nav. property's names the only you need to do is this:

 //...
 var navProperties = set.EntitySet.ElementType.NavigationProperties.Select(np=>np.Name);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!