Skip to content

Instantly share code, notes, and snippets.

@raghuramn
Created June 26, 2013 01:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raghuramn/5864013 to your computer and use it in GitHub Desktop.
Save raghuramn/5864013 to your computer and use it in GitHub Desktop.
public static IEdmModel GetEdmModel(this DbContext context)
{
using (MemoryStream stream = new MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create(stream))
{
EdmxWriter.WriteEdmx(context, writer);
writer.Close();
stream.Seek(0, SeekOrigin.Begin);
using (XmlReader reader = XmlReader.Create(stream))
{
return Microsoft.Data.Edm.Csdl.EdmxReader.Parse(reader);
}
}
}
}
@Chrille79
Copy link

Seems that the example above produces wrong namespaces.

I got the entity context in App.Data and the models in App.Models and WepApi in App.Web
The Schema renders the Models in App.Data
By convert the stream to a string and do a Replace("App.Data", "App.Models") then it works fine.

Please fix the sample above

@Chrille79
Copy link

The Bad fix

    public static IEdmModel GetEdmModel(this AppDbContext context)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            using (XmlWriter writer = XmlWriter.Create(stream))
            {
                EdmxWriter.WriteEdmx(context, writer);
                writer.Close();
                stream.Seek(0, SeekOrigin.Begin);

                var sr = new StreamReader(stream);
                var myStr = sr.ReadToEnd();
                myStr = myStr.Replace("App.Data", "App.Models");

                using (XmlReader xmlReader = XmlReader.Create(new StringReader(myStr)))
                {
                    return Microsoft.Data.Edm.Csdl.EdmxReader.Parse(xmlReader);
                }
            }
        }
    }

@AdamCaviness
Copy link

@raghuramn, Could we get a new sample of this that works with the latest bits on MyGet (EdmLib 6.4)? I arrived here via this work item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment