Code First and Other ED Features?

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

Code First and Other ED Features?

Post by CodeSlinger » Mon 04 May 2015 19:25

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

CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

Re: Code First and Other ED Features?

Post by CodeSlinger » Mon 04 May 2015 20:54

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.

CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

Re: Code First and Other ED Features?

Post by CodeSlinger » Tue 05 May 2015 12:38

Does this question belong here or in the Entity Developer forum since using ED with EF?

Thanks, Dave

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Re: Code First and Other ED Features?

Post by Shalex » Tue 05 May 2015 13:41

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.

CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

Re: Code First and Other ED Features?

Post by CodeSlinger » Tue 05 May 2015 14:07

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

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Re: Code First and Other ED Features?

Post by Shalex » Wed 06 May 2015 12:52

Could you please specify what code generation you expect to see in Entity Developer to fulfill your task?

CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

Re: Code First and Other ED Features?

Post by CodeSlinger » Thu 07 May 2015 18:07

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.

Post Reply