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

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

问题描述

在项目中,我使用实体框架6.
我有这些实体:

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; }
    }

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

        public string Name { get; set; }
    }

当我插入一个人对象,填写PersonRights它看起来像这样的数据库:

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

表Person实体:

dbo.People with columns Id, Name

表PersonRights实体

table for PersonRights entity

dbo.PersonRights with columns Id, Name, Person_Id



当我从它未装满PersonRights,因为这是enabeling延迟加载功能的虚拟关键字属性数据库加载一个人 - 和它的好

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.

然后我得到的为人员对象PersonRights,它也能正常工作。

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

事情是,因为在PersonRight实体没有导航属性,实体框架必须由哪些列知道数据库是这两个属性的限制。
在数据库疗法是一个外键连接PersonRights和人民表:

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

现在的问题是:有没有办法如何让列名由连接这两个属性? ?
任何路怎么走在串码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

非常感谢您的回答:)

编辑:

好吧,我发现该列欢迎使用属性的名称是在这里:

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

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



但我仍然无法达到目标,这个问题很奇怪,当我拿到第一个项目,从这个集合它让我发现,它的类型是 System.Data.Entity.Core.Mapping.StorageEntityContainerMapping
,但是当我通过它通过的foreach突然类型是 System.Data.Entity.Metadata.Edm.GlobalItem ...

我如何访问系统.Data.Entity.Core.Mapping.StorageEntityContainerMapping 项,其中还我需要得到一个名为列集合 - AssociationSetMappings ??

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 ??

推荐答案

您可以从存储模型的实际字符串PERSON_ID,但你不能确定财产/列作为外键。对于您需要PERSON_ID概念模型存在。我还是不太明白,为什么你不希望它在模型中,但这里是你将如何从存储元数据得到它:

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天全站免登陆