David Catriel
David Catriel

Reputation: 375

Retrieving column mapping info in T4

I'm working on a T4 file that generates .cs classes based on an entity model, and one of the things I'm trying to get to is the mapping info in the model. Specifically, for each field in the model I'm trying retrieve the database field name it is mapped to.

I've found that the mapping info is apparently stored in StorageMappingItemCollection, but am having an impossible time figuring out how to query it and retrieve the data I need. Has anyone worked with this class and can maybe provide guidance?

The code I have so far goes something like this (I've pasted everything up to the problematic line):

    <# 
        System.Diagnostics.Debugger.Launch(); 
        System.Diagnostics.Debugger.Break();
    #>

    <#@ template language="C#" debug="true" hostspecific="true"#>
    <#@ include file="EF.Utility.CS.ttinclude"#>
    <#@ output extension=".cs"#><#

    CodeGenerationTools code = new CodeGenerationTools(this);
    MetadataLoader loader = new MetadataLoader(this);
    CodeRegion region = new CodeRegion(this, 1);
    MetadataTools ef = new MetadataTools(this);

    string inputFile = @"MyModel.edmx";
    EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
    StoreItemCollection storeItemCollection = null;
    loader.TryCreateStoreItemCollection(inputFile, out storeItemCollection);
    StorageMappingItemCollection storageMappingItemCollection = null;
    loader.TryCreateStorageMappingItemCollection(
inputFile, ItemCollection, storeItemCollection, out storageMappingItemCollection);

    var item = storageMappingItemCollection.First();

storageMappingItemCollection has methods like GetItem() and such, but I can't for the life of me get it to return data on fields that I know exist in the model.

Thx in advance!

Upvotes: 3

Views: 1040

Answers (2)

JJS
JJS

Reputation: 6678

Parsing the MSL isn't really that hard with Linq to XML

string mslManifestResourceName = GetMslName(ConfigurationManager.ConnectionStrings["Your Connection String"].ConnectionString);
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(mslManifestResourceName);
XmlReader xreader = new XmlTextReader(stream);
XDocument doc = XDocument.Load(xreader);
XNamespace xmlns = "http://schemas.microsoft.com/ado/2009/11/mapping/cs";

var items = from entitySetMap in doc.Descendants(xmlns + "EntitySetMapping")
            let entityTypeMap = entitySetMap.Element(xmlns + "EntityTypeMapping")
            let mappingFragment = entityTypeMap.Element(xmlns + "MappingFragment")
            select new
            {
                EntitySet = entitySetMap.Attribute("Name").Value,
                TypeName = entityTypeMap.Attribute("TypeName").Value,
                TableName = mappingFragment.Attribute("StoreEntitySet").Value
            };

Upvotes: 2

podiluska
podiluska

Reputation: 51504

It may be easier to parse the EDMX file as XML rather than using the StorageMappingItemCollection.

Upvotes: 0

Related Questions