Page 1 of 1

Code First and Other ED Features?

Posted: Mon 04 May 2015 19:25
by CodeSlinger
We have been using EF through several versions with SQL Server. We started with Model First since Code First was terrible early on, then Database First when Model First did not work well with views and sprocs, and now EF7 says both will be dropped so we are considering migrating to Code First in EF6 before eventually converting to EF7. Still I want a visual designer for creating and understanding relationships so am considering DevArt's Entity Developer, LLBLGen, and Sparx Enterprise Architect w/ transforms for our development team going forward.

Mainly I'm wondering if the EF7 announcment of dropping Model First and Database First has changed future plans for Entity Developer or if you plan any Code First generation and support. Some DevArt ED questions I have are -

- Can I via code specify different schema concurrently for the same DbContext? We use this technique to clone and update large tables in the background, and then swap using schema transfers and that works well and quickly. I have this working in Database First currently but it is ugly compared to how I can do this in Code First.

- Can I add my own model Data Annotation decorations to the models like I can in Code First (e.g. Max/min length, ErrorMessage, COncurrencyCheck, NotMapped, Index, Required, validation etc.)?

- Can I use multiple DbContext for the same database, each with only the needed tables and relationships, for a large database, so I don't have to define the entire infrastructure for a very small DbContext we want to run quickly? Don't want to create 100 tables and relationships just because I lookup in two related tables.

- Can I define my own partial classes? Use class and interface inheritance?

Also, do your Model First and Database First approaches "round trip"? Iow, can I switch between using DF and CF as I like on the same project as appropriate? E.G.

- Use DF for bringing in a new view
- Then use MF for defining a new table and relationship

Thanks. A prospective customer, Dave

Re: Code First and Other ED Features?

Posted: Mon 04 May 2015 20:54
by CodeSlinger
Also nice would be some sort of general overview on what DevArt brings to the table in a Code First environment and how the Entity Designer fits into the picture, and if the MF, DF and CF approaches can be used interchangeably (round tripping) in the same project which would be awesome.

Re: Code First and Other ED Features?

Posted: Tue 05 May 2015 12:38
by CodeSlinger
Does this question belong here or in the Entity Developer forum since using ED with EF?

Thanks, Dave

Re: Code First and Other ED Features?

Posted: Tue 05 May 2015 13:41
by Shalex
CodeSlinger wrote:can I switch between using DF and CF as I like on the same project as appropriate? E.G.
- Use DF for bringing in a new view
- Then use MF for defining a new table and relationship
Yes, you can.
Entity Developer includes both features: Update Model From Database (DF) and Update Database From Model (MF). Additionally, Entity Developer is supplied with an enhanced DbContext template which allows to use fluent mapping not only with CF approach but with DF and MF as well.
CodeSlinger wrote:- Can I via code specify different schema concurrently for the same DbContext?
Entity Developer allows editing SSDL in design time. So you may specify the Schema SSDL property of your class, then Devart's DbContext template will generate the corresponding attribute in the code.
Also be aware about the "Preserve schema name in storage" option in Model Settings. It is used in DF.
CodeSlinger wrote:- Can I add my own model Data Annotation decorations to the models like I can in Code First (e.g. Max/min length, ErrorMessage, COncurrencyCheck, NotMapped, Index, Required, validation etc.)?
Yes, you can.
There are two ways to add attributes via interface of Entity Developer:
1. Validation Framework
a) open Tools > Entity Developer > Model Explorer > the Templates node, select your template, navigate to its properties:
-> set the Validation Framework property (e.g., to "Data Annotations")
-> open Validation Error Messages and specify the text for Required Message
b) set Validate Required=True for a particular class property
2. Custom Attributes
a) navigate to Model > Settings > Attributes > select the assembly with the needed attributes and make sure that the needed attributes are checked in the window below, press OK
b) select the needed attribute in the Attributes collection of a particular class property
The [1] approach is easier to use. If [1] doesn't cover your needs, you can add attributes in the way which is described in [2].
CodeSlinger wrote:- Can I use multiple DbContext for the same database, each with only the needed tables and relationships, for a large database, so I don't have to define the entire infrastructure for a very small DbContext we want to run quickly? Don't want to create 100 tables and relationships just because I lookup in two related tables.
You can create any number of *.edml models in your project.
General information about performance considerations with Entity Framework: https://msdn.microsoft.com/en-us/library/cc853327.aspx.
Entity Developer allows to implement:
CodeSlinger wrote:- Can I define my own partial classes? Use class and interface inheritance?
Yes, you can.
For this, set the Generate Partial Class property of the DbContext template to True. More information about this property is available in its description via interface of Entity Developer.

JIC: Entity Developer allows only to import *.edmx but the output would be *.edml. Output can not be *.edmx because Entity Developer uses extra tags and attributes, additional features which are not supported by EDM Designer. This question was discussed at http://forums.devart.com/viewtopic.php?f=32&t=24766.

The Entity Developer documentation can be downloaded here.

Re: Code First and Other ED Features?

Posted: Tue 05 May 2015 14:07
by CodeSlinger
This is all good news. I have to clarify what I meant by dynamically setting the schema however. When I create my model, it uses the default dbo schema which is the only schema I import tables from. When I update certain R/O pricing tables that are large and which are actively read, the trick I use right now, is to use a sql script to clone (copy) these tables into a new schema (e.g. D20150504), then use the same DbContext created for dbo with the new schema name by editing the underlying edmx xml in memory so effectively synamically switch the schema name, then alter the schema to move the dbo tables to yet another schema for temporary backups, and move the new tables from the new schema to the production dbo schema. This lets me update the tables w/o bothering production transactions as updating takes a while and I don't want to lock out transactions.

This is all done a lot easier in Code First OnModelCreating than what I do now with Model First or Database First where EF reads the edmx xml to setup the environment and where I have to myself modify the edmx file data in memory before creating the DbContext dynamically from that in-memory data (http://stackoverflow.com/questions/2476 ... -delegates).

So I'm wondering if Entity Designer using MF, DF or CF would somehow allow me to use the model generated based on the dbo schema and dynamically create a DbContext for a different schema than dbo so I can get at the cloned tables that were identical in regards to the model but using a different schema name than dbo.

Thanks, Dave

Re: Code First and Other ED Features?

Posted: Wed 06 May 2015 12:52
by Shalex
Could you please specify what code generation you expect to see in Entity Developer to fulfill your task?

Re: Code First and Other ED Features?

Posted: Thu 07 May 2015 18:07
by CodeSlinger
I managed to convert my EDM Designer DF project over to DevArt EntityDeveloper after figuring out how to add enums as external and renamed a few navigation properties since your naming conventions were different than MSFT's. So currently we are using DF output to edml.

I want to experiment with CF output going forward if I get more feature out of it such as more efficiently setting the alternate schema name I want to use for the model.