如何映射回从模型在MVC除了得到实际的SQL表的列名? [英] how to map back to get the actual sql table column name apart from model in MVC?

查看:94
本文介绍了如何映射回从模型在MVC除了得到实际的SQL表的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的映射表列名一般的方式在模型中使用它们。
这样的:

I am mapping my table column name to generic way in to use them in model. like:

        public UserEntityMapper()
        {
            ToTable("tbl_User");

            HasKey(m => m.UserKey);
            Property(p => p.InsuredKey).HasColumnName("User_KEY");
            Property(p => p.FirstName).HasColumnName("FIRST_NAME");
        }

IIF我知道使用模型名字字段已被修改的页面上,那么就需要知道,在SQL表中的列名是将First_Name

Iif i know FirstName field has been modified on the page using model, then need to know that the column name in the sql table is 'First_Name'

反正接近它。

感谢

推荐答案

我已经看到了之前类似的问题,比满足基于分裂由 DbSet 生成的SQL字符串解决几个不大。试图访问映射元数据是由不可访问的内部类型阻止。它不停地(并保持)缠着我,这个信息如此隐蔽。东北黑钙土一切,这是你自己的映射。

Reading the mappings

I've seen similar questions before and several less than satisfying solutions based on splitting the SQL string generated by a DbSet. Attempts to access the mapping metadata are blocked by unaccessible internal types. It kept (and keeps) bugging me that this information is so hidden. Atter all, it's your own mapping.

所以,我做了一些鬼混,看看是否能找到一些改善这里。因为一切都可以很容易地从EDMX文件中读取,这就是我开始。终于到了那里。它下面code时,EDMX文件在的XDocument 名为商务部。我将解释下面的code。

So I did some fooling around to see if something can be improved here. Since everything can easily be read from an edmx file, that's where I started. And finally got there. It the following code, the edmx file is in an XDocument called doc. I'll explain the code below.

var xn = XName.Get("Mappings", doc.Root.GetDefaultNamespace().ToString());
var mappingsNode = doc.Descendants(xn).Descendants().First();
var mappingDoc = XDocument.Parse(mappingsNode.ToString());
xn = XName.Get("MappingFragment", mappingDoc.Root
                                            .GetDefaultNamespace().ToString());
var mappings = mappingDoc
            .Descendants(xn)
            .Select(x => new
                {
                    Entity = x.Attribute("StoreEntitySet").Value,
                    Mapping = x.Descendants()
                                .Select(d => new 
                                            { 
                                              Property = d.Attribute("Name")
                                                          .Value,
                                              Column = d.Attribute("ColumnName")
                                                        .Value
                                            })
                });

这是一个非常小的EDMX输出示例:

Sample output from a very small edmx:

Entity      Property        Column
---------------------------------------------
Category    CategoryID      CategoryID 
            Name            CategoryName 
            Description     Description 

Product     ProductID       ProductID 
            Name            ProductName 
            QuantityPerUnit QuantityPerUnit 
            UnitPrice       UnitPrice 
            StartDate       StartDate 

说明:

这是EDMX文件有我在这里总结的固定结构:

Explanation:

An edmx file has a fixed structure that I summarize here:

<Edmx Version='3.0' xmlns='http://schemas.microsoft.com/ado/2009/11/edmx'>
  <Runtime>
    <ConceptualModels>
    ...
    </ConceptualModels>
    <Mappings>
      <Mapping Space='C-S' xmlns='http://schemas.microsoft.com/ado/2009/11/mapping/cs'>
        <EntityContainerMapping StorageEntityContainer='CodeFirstDatabase' CdmEntityContainer='UserQuery'>
          <EntitySetMapping Name='Categories'>
            <EntityTypeMapping TypeName='CodeFirstNamespace.Category'>
              <MappingFragment StoreEntitySet='Category'>
                <ScalarProperty Name='CategoryID' ColumnName='CategoryID' />
                ...
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <AssociationSetMapping Name='Category_Products' TypeName='CodeFirstNamespace.Category_Products' StoreEntitySet='CategoryProduct'>
          </AssociationSetMapping>
        </EntityContainerMapping>
      </Mapping>
    </Mappings>
    <StorageModels>
    ...
    </StorageModels>
  </Runtime>
  <Designer>
  ...
  </Designer>
</Edmx>

映射部分就是我们所追求的。所以我首先创建一个新的的XDocument 这部分的:

The Mappings part is what we're after. So I first create a new XDocument of this part:

var xn = XName.Get("Mappings", doc.Root.GetDefaultNamespace().ToString());
// First descendant of the "Mappings" node, which is the "Mapping" node
var mappingsNode = doc.Descendants(xn).Descendants().First();
var mappingDoc = XDocument.Parse(mappingsNode.ToString());

映射节点都有自己的命名空间,所以我分析它变成一个的XDocument 为了得到这个名称空间由我可以查询它的节点。因此,我可以得到映射片段:

The "Mapping" node has its own namespace, so I parse it into an XDocument in order to get this namespace by which I can query its nodes. Thus, I can get to the mapping fragments:

xn = XName.Get("MappingFragment", mappingDoc.Root
                                            .GetDefaultNamespace().ToString());
var mappings = mappingDoc.Descendants(xn) ...

也许有一个更好的方式来获得这些节点,但我不是在这个的XDocument API非常流畅。

然后,它的拉出右属性和投射它们可查询结构的问题。

Then it's a question of pulling out the right attributes and project them to a queryable structure.

它的工作原理,OK,但它不是高雅的。它极大地取决于EDMX结构,因此它可以打破任何时刻。我觉得应该来一个像样的,方便的方式来阅读上下文的元数据。

It works, OK, but it's not elegant at all. It greatly depends on the edmx structure, so it can break any moment. I think there should come a decent, convenient way to read a context's metadata.

顺便说一句,如果你工作code-首先,你可以生成由一个EDMX的 EdmxWriter

By the way, if you work code-first you can generate an edmx by an EdmxWriter.

这篇关于如何映射回从模型在MVC除了得到实际的SQL表的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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