beaucrawford.net

Give me data or give me death

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Don't show

Authors

Tags

Don't show

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    Entity Framework - Generating SSDL, CSDL, and MSL

    If you do not want to use the Visual Studio designer to generate your Entity Framework schema files you can use edmgen instead.  I spent some time poking around this application in Reflector and found out what’s involved.  You can use this code to generate the Entity Framework schema files directly yourself.

    Note: You will first need to reference the "System.Data.Entity.Design" assembly and namespace.

    Generating the SSDL file:

    const string Provider = "System.Data.SqlClient";
    const string ConnectionString = "server=.;database=MyDatabase;integrated security=true;";
    const string Namespace = "Testing";
    
    var generator = new EntityStoreSchemaGenerator(Provider, ConnectionString, Namespace);
    generator.GenerateStoreMetadata();
    generator.WriteStoreSchema(@"D:\temp\storeSchema.ssdl");
    var storeEntityContainer = generator.EntityContainer;

    Generating the CSDL and MSL files:
    const string Namespace = "Testing";
    const string ContainerName = "MyContainer";
    
    EntityModelSchemaGenerator generator = new EntityModelSchemaGenerator(storeEntityContainer, Namespace, ContainerName);
    generator.GenerateMetadata();			
    generator.WriteModelSchema(@"D:\temp\modelSchema.csdl");
    generator.WriteStorageMapping(@"D:\temp\storageMapping.msl");


    Generate Classes:

    private static void GenerateObjectLayerCode(string csdlFile, IEnumerable additionalSchemas)
    {
    	var generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode);
    	var reader = XmlReader.Create(csdlFile);
    	var writer = new StringWriter();
    	generator.GenerateCode(reader, writer, additionalSchemas);
    	string code = writer.GetStringBuilder().ToString();
    }

    The disappointment is that the extensibility for generating the classes in the last step is extremely limited. You only have two options – implement event handlers that fire when a Type and/or Property is created.  The problem is that the TypeGeneratedEventArgs and PropertyGeneratedEventArgs classes passed to your event handler are extremely limited.  For the properties you are not passed the entire CodeDOM CodeMemberProperty instance.  Instead you are only allowed to add additional get/set statements.  You have no way of completely customizing the property definition. In a future post I plan to explore the usage of NRefactory to accomplish this.

    Posted by Beau on Saturday, February 21, 2009 3:00 PM
    Permalink | Comments (2) | Post RSSRSS comment feed

    StyleCop and Partial Methods


    I added a partial method to some code today and StyleCop decided that it didn’t like the fact that my method did not have an access modifier.  The error I received from StyleCop was:

    SA1205: The partial method does not have an access modifier defined. StyleCop may not be able to determine the correct placement of the elements in the file. Please declare an access modifier for all partial methods.

    Well, as you probably know, access modifiers are not valid on partial methods.  I figured this was probably a bug in StyleCop and decided to try to simply ignore it.  I fired up the StyleCop UI only to discover that SA1205 cannot be ignored (actually, it doesn’t even show up). Bummer.  I poked around a little bit and stumbled across this post.  It seems that the rule that is causing this problem is loaded from an XML file that is an embedded resource.  This file controls whether rules can be disabled.  Well, hoping that they maybe “locked the front door but not the back door” I tried to add it to my .StyleCop settings file directly with:

    <Analyzer AnalyzerId="Microsoft.StyleCop.CSharp.OrderingRules">
      <Rules>
        <Rule Name="PartialElementsMustDeclareAccess">
          <RuleSettings>
            <BooleanProperty Name="Enabled">False</BooleanProperty>
          </RuleSettings>
        </Rule>
    </Analyzer>

    That didn’t work either.  Damn.  Well, given the fact that we treat StyleCop violations as errors, I decided to redesign my code to not use partial methods.  Style over function, I guess.


    Update: Looks like StyleCop 4.3.1.3 has fixed this bug.


    Categories: C#
    Posted by Beau on Thursday, February 19, 2009 11:11 PM
    Permalink | Comments (0) | Post RSSRSS comment feed

    MSBuild Batching


    I banged my head on this one for a little bit but then finally found the answer here.  You sometimes find yourself in a situation where you would like “batch” items from an ItemGroup.  To do so you simply need to set your Target’s Outputs attribute to contain the identity of the item.  This will execute the entire target once per item in the group.

    Example:

    <ItemGroup>
        <TestItem Include="test A" />
        <TestItem Include="test B" />
        <TestItem Include="test C" />
    </ItemGroup>

    <Target Name="Build" Outputs="%(TestItem.Identity)">
        <Message Text="%(TestItem.Identity)" />
    </Target>

    Enjoy!

    Categories: MSBuild
    Posted by Beau on Thursday, February 12, 2009 11:53 PM
    Permalink | Comments (0) | Post RSSRSS comment feed