SAP NetWeaver Gateway provides many useful generator tools which allow you to develop services around RFCs, BAPIs, Dynpro screens, etc. While this inside-out development approach certainly speeds up the development cycle, it is not without its limitations. Whenever you run up against these limitations, you can turn to the OData Channel API which offers full control over the OData generation process. However, the devil to this outside-in approach is in the entity modeling details.
If you’re fortunate to have a copy of Visual Studio.NET 2010 lying around, then you can bypass a lot of this minutia by creating Entity Data Models using a WYSIWYG editor. These models can then be imported into the system using the OData Channel Generation Tool provided with the SAP NetWeaver Gateway toolset. Fortunately, for those of us that are too cheap to spring for Visual Studio.NET 2010 or too impatient for SAP to deliver an EDM modeling tool for Eclipse/NWDS, there is another alternative. With the freely distributable .NET Framework 4.x, there is a tool called EdmGen.exe which is able to build EDM models off of just about any database which can be accessed using ADO. In this blog entry, I’ll demonstrate the use of this tool.
Setup and Prerequisites
As mentioned earlier, the EdmGen.exe tool is bundled with .NET Framework 4.x. So, if you don’t have that installed already, you can download it here. In addition to this, you also need to download a companion tool called EdmGen2.exe off the MSDN. The download archive contains the complete source code for the solution, but you can access the binary in the bin/Debug folder. Finally, you’ll need a database in which to perform your modeling. For the purposes of this demonstration, I have downloaded the freely available MySQL RDBMS along with its graphical editor tool, MySQL Workbench. If you prefer to work with a different RDBMS, you can likely find documentation for its usage with the EdmGen.exe tool online.
Conceptually speaking, EDM models are not unlike ER models you’ve likely worked with before. Here, we’re basically talking about modeling entities and defining their relationships. With the OData Channel API, you can define the model statically in code like this. However, this process is tedious and error prone. It is much more efficient and intuitive to model the entities using graphical tools. Naturally, the built-in modeling tools bundled with many RDBMS systems fit this bill nicely. For example, the screenshot below shows how I have modeled a couple of entities called Bank and BankAddress using the MySQL Workbench. These two entities have a 1..1 entity relationship defined via foreign keys. Of course, we could have defined a much more complex entity model, but you get the basic idea.
Generating the EDMX File
Once you have modeled your entities in your RDBMS of choice, the next step is to use EdmGen.exe to introspect this model. For my MySQL database, I was able to achieve this via the command line as follows:
“%windir%\Microsoft.NET\Framework\v4.0.30319\edmgen.exe” /mode:FullGeneration /project:Bank /provider:MySql.Data.MySqlClient /c:”server=localhost;User Id=root;Password=nwgateway;Persist Security Info=True;Connect Timeout=300000;Default Command Timeout=300000;database=nwgateway” /entitycontainer:Banks /namespace:BankModel /language:CSharp
This command generated a series of artifacts, most notably:
- A Bank.csdl file
- A Bank.msl file
- And a Bank.ssdl file
To combine these artifacts into an EDMX file, I used EdmGen2.exe like this:
“%windir%\Microsoft.NET\Framework\v4.0.30319\edmgen2.exe” /ToEdmx Bank.csdl Bank.msl Bank.ssdl
Once this is complete, I have an EDMX file called Bank.edmx. You can view this file in a text editor to see how the entities are specified, etc.
Importing the Model
After you generate your EDMX file, the last step is to import the model into the backend system. This can be achieved via Transaction /IWBEP/IMPORT_MODEL as shown below.
When prompted, select your .edmx file and then you’ll be routed to the generation screen shown below:
After any last minute customizations, you can click on the Generate button to generate the model. Assuming all goes well, the target model/data classes will be generated and you should be ready to start coding. However, by using the wizard-based approach, you already have a leg up as the code generators have already taken care of specifying the data model, etc.
Where to Go From Here
Admittedly, the modeling approach described is this blog post is a hack. In the near future, SAP has indicated that they will provide an Eclipse-based toolset which will likely be MUCH more elegant and efficient to the methods described here. In the meantime, hopefully this stopgap approach will serve you well.