one.beat.consumer
one.beat.consumer

Reputation: 9504

Using existing C# model classes to build an entity diagram (.EDMX)

I am porting an application's persistent storage mechanics from SQL Compact to Oracle.

Current Implementation

The application implements Entity Framework 5 (EF), but it is pretty tightly coupled: (a) there is no service layer between EF and the Controllers, and (b) there is no IRepository-style abstraction between EF and SQL Compact.

The C# models are simple POCOs but are rich with data annotations, and is used "code-first" style to generate DB schema.

Problem (Oracle)

I must use Oracle's standard ODP.Net provider. The provider supports EF, but does not support "code-first" methodology, only "model first".

Intended Solution

I am trying to find a way to preserve the EF implementation tightly coupled to the controllers, and the richness of the model class annotations, while using them to build the "model-first" style EDMX file that Oracle can swallow. Essentially, I am looking for a way to mimic "code-first".

I was hoping it would be as simple as dragging my C# objects onto the EDMX file but it seems like there's much more going on.

Question

How can I build an .EDMX file using C# models that already exist? If it is not possible, what is the next best solution for accomplishing this goal?

Note: Please do not recommend paid-for third party products. The solution must be doable with Visual Studio, C#, an open source library, or some combination.

Upvotes: 3

Views: 1866

Answers (3)

Rick Petersen
Rick Petersen

Reputation: 744

Edit: The place that article was posted reorganized their URLs. Fixed the reference. Sorry for the broken link.

Here is a blog post that I wrote that explains how to use the MetaDataTypeAttribute to allow you the benefit of all the code-first annotation work when utilizing a newly-generated database-first/model-first edmx.

While it won't let you build the .edmx from the models, you can at least generate from database and then use the current code-first classes as metadata.

Upvotes: 0

one.beat.consumer
one.beat.consumer

Reputation: 9504

@Pawel provided the the underlying answer, EdmxWriter. His answer is marked and thanks go to him. My answer is just to provide more detail, a reference to Oracle documentation, and a simple code example:


C# to EDMX

The System.Data.Entity.Infrastructure namespace provides a static EdmxWriter for this purpose:

// 1. Instantiate your DbContext derived type
var db = new AppContext();

// 2. Instantiate an XmlWriter
var xw = XmlWriter.Create("models.edmx");

// 3. Write to file.
EdmxWriter.WriteEdmx(db, xw);

The resulting EDMX file can be added to the project/solution and opened with the designer.

EDMX to Oracle Schema

With a few changes to the EDMX file properties (see "Model First" Step 2), you can then Generate Database from Model to the Oracle-syntax SQL scripts used to build the db schema.

Note: As Pawel noted in comment, the EDMX writer will acknowledge and respect both data annotation attributes (ex. [MaxLength]) and fluent api configs .HasMaxLength(50), but it does not handle certain data type mapping issues between C#/EntityFramework and Oracle's db types.

Upvotes: 0

Pawel
Pawel

Reputation: 31610

If you have DbContext you can use 'EdmxWriter.WriteEdmx()' to write your model as the edmx file that you should be able to open with the designer.

Upvotes: 1

Related Questions