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

    Comments

    Gutemberg Ribeiro br

    Thursday, June 18, 2009 6:18 PM

    Hello Beau!

    Thanks for the post with this code. It help me a lot.

    Regarding this generation of the Entity Model files, I'm with some problems using the generated models inside POC Adapter for Entity Framework.

    I posted it into MSDN Code discussion list of the project.

    Please if u have time, can u take a look on this problem? code.msdn.microsoft.com/.../View.aspx

    Thanks a lot for the help. I really appreciate it.

    Cya...

    Gutemberg Ribeiro

    Durai karthik in

    Friday, July 10, 2009 2:35 AM

    Initially i have developed the ADO.NET Entity Data Model against MSSQL Server database . It works fine and i have app.config file like this. Given provider is System.Data.SqlClient.

    <add name="GTechEntities" connectionString="metadata=res://*/BusinessFrameworkModel.csdl|res://*/BusinessFrameworkModel.ssdl|res://*/BusinessFrameworkModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=172.16.0.7;Database=GTech;Persist Security Info=True;User ID=gtech;Password=gtech123;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

    Now i am trying to connect my windows .Net application with Oracle 9i databse with the following changes made in app.config. Just given provider as System.Data.OracleClient .

    <add name="GTechEntities" connectionString="metadata=res://*/BusinessFrameworkModel.csdl|res://*/BusinessFrameworkModel.ssdl|res://*/BusinessFrameworkModel.msl;provider=System.Data.OracleClient;provider connection string=&quot;server=172.16.0.248;Data Source=GTech;Persist Security Info=True;uid=system;pwd=gtech456;&quot;" providerName="System.Data.EntityClient" />

    It gives an error like this ,

    The 'System.Data.SqlClient' provider from the specified SSDL artifact(s) does not
    match the expected 'System.Data.OracleClient' provider from the connection string.


    I heared the ADO.NET Entity Data Model offers provider Model. Hope application can be developed initially against MSSQL server and later on switch to MySQL without any code changes

    Kindly advice.