在实体框架中获取属性的列名称 [英] Getting mapped column names of properties in entity framework

查看:142
本文介绍了在实体框架中获取属性的列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用Entity Framework 6.
我有这些实体:

  public class Person 
{
[Key]
public int Id {get;组; }

public string Name {get;组; }

public virtual ICollection< PersonRight> PersonRights {get;组;
}

  public class PersonRight 
{
[Key]
public int Id {get;组; }

public string Name {get;组; }
}

当我插入一个填充PersonRights的人物时,它看起来像这样数据库:



个人实体表:

  dbo.People对于PersonRights实体,列ID,名称

  dbo.PersonRights带有列ID,名称,Person_Id 

当我从一个数据库加载一个人,它没有填充PersonRights属性,因为虚拟关键字,这是懒惰的加载功能 - 并且它的好。



然后我得到人物对象的PersonRights也可以正常工作。



事情是,由于PersonRight实体中没有导航属性,实体框架必须知道数据库是这两个属性有界的。
在数据库中是连接PersonRights和People表的外键:

  FK_dbo.PersonRights_dbo.People_Person_Id 

问题是:有没有什么方法如何获取这两个属性连接的列名?
任何方式如何在代码中获取字符串Person_Id?



有一种方法如何找出哪个表是一个在数据库中有界的实体:



http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent



非常感谢您的答案:)



编辑:



发现列名称是在这里:

  var items =((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace.GetItems DataSpace.CSSpace); 

但是我仍然无法达到它,问题很奇怪,当我收集这个集合的第一个项目告诉我,它的类型是 System.Data.Entity.Core.Mapping.StorageEntityContainerMapping
但是当我通过foreach突然看到它的类型是$ code> System.Data.Entity.Metadata.Edm.GlobalItem ...



如何访问系统.Data.Entity.Core.Mapping.StorageEntityContainerMapping 项目也是我需要获取名为 - AssociationSetMappings ??

解决方案

您可以从存储模型获取实际的字符串Person_Id,但不能将该属性/列标识为外键。为此,您需要在概念模型中存在Person_Id。我仍然不太明白为什么你不想在模型中,但是这里是如何从存储元数据中获取的:

  using(var context = new YourEntities())
{
var objectContext =((IObjectContextAdapter)context).ObjectContext;
var storageMetadata =((EntityConnection)objectContext.Connection).GetMetadataWorkspace()。GetItems(DataSpace.SSpace);
var entityProps =(来自storageMetadata中的s,其中s.BuiltInTypeKind == BuiltInTypeKind.EntityType select s为EntityType);
var personRightStorageMetadata =(from m in entityProps where m.Name ==PersonRightselect m).Single();
foreach(var item in personRightStorageMetadata.Properties)
{
Console.WriteLine(item.Name);
}
}


in my project I use Entity Framework 6. I have these entities:

   public class Person
    {
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }

        public virtual ICollection<PersonRight> PersonRights { get; set; }
    }

and

 public class PersonRight
    {
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }
    }

When I insert a person object with filled in PersonRights it looks like this in the database:

table for Person entity:

dbo.People with columns Id, Name

table for PersonRights entity

dbo.PersonRights with columns Id, Name, Person_Id

when I load a person from a database it hasnt filled PersonRights property because of the virtual keyword which is enabeling the lazy loading feature - and its okay.

Then I get the PersonRights for the person object and it also works fine.

The thing is, since there is no navigation property in PersonRight entity, the entity framework must know by which columns in the database are those two properties bounded. In database ther is a foreign key connecting PersonRights and People tables:

FK_dbo.PersonRights_dbo.People_Person_Id

The question is : Is there any way how to get the column name by which are those two properties connected? Any way how to get the string "Person_Id" in code?

There is a way how to find out to which table is an entity bounded in database :

http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

thanks a lot for your answers :)

EDIT:

Well I found out that the column name propety is here:

  var items = ((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace.GetItems(DataSpace.CSSpace);

but I still cant reach it, the problem is weird, when I get first item from this collection it shows me that its type is System.Data.Entity.Core.Mapping.StorageEntityContainerMapping but when I go through it by foreach suddenly the type is System.Data.Entity.Metadata.Edm.GlobalItem ...

How can I access the System.Data.Entity.Core.Mapping.StorageEntityContainerMapping item where is also the collection I need to get the column named - AssociationSetMappings ??

解决方案

You can get to the actual string "Person_Id" from the storage model, but you cannot identify that property/column as the foreign key. For that you would need Person_Id to exist in the conceptual model. I still don't quite understand why you wouldn't want it in the model, but here's how you would get it from the storage metadata:

using ( var context = new YourEntities() )
{
  var objectContext = ( ( IObjectContextAdapter )context ).ObjectContext;
  var storageMetadata = ( (EntityConnection)objectContext.Connection ).GetMetadataWorkspace().GetItems( DataSpace.SSpace );
  var entityProps = ( from s in storageMetadata where s.BuiltInTypeKind == BuiltInTypeKind.EntityType select s as EntityType );
  var personRightStorageMetadata = ( from m in entityProps where m.Name == "PersonRight" select m ).Single();
  foreach ( var item in personRightStorageMetadata.Properties )
  {
      Console.WriteLine( item.Name );
  }
}

这篇关于在实体框架中获取属性的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆