Sunday, 19 January 2014

Basics of Entity Framework–Part 2




image




image


Note:
A mental model captures ideas in a problem domain, while a conceptual model represents 'concepts' (entities) and relationships between them. A Conceptual model in the field of computer science is also known as a domain model. Conceptual modeling should not be confused with other modeling disciplines such as data modelling, logical modelling and physical modelling. The conceptual model is explicitly chosen to be independent of design or implementation concerns, for example, concurrency or data storage. The aim of a conceptual model is to express the meaning of terms and concepts used by domain experts to discuss the problem, and to find the correct relationships between different concepts. The conceptual model attempts to clarify the meaning of various, usually ambiguous terms, and ensure that problems with different interpretations of the terms and concepts cannot occur. Such differing interpretations could easily cause confusion amongst stakeholders, especially those responsible for designing and implementing a solution, where the conceptual model provides a key artifact of business understanding and clarity. Once the domain concepts have been modeled, the model becomes a stable basis for subsequent development of applications in the domain. The concepts of the conceptual model can be mapped into physical design or implementation constructs using either manual or automated code generation approaches. The realization of conceptual models of many domains can be combined to a coherent platform.
A conceptual model can be described using various notations, such as UML, ORM or OMT for object modelling, or IE or IDEF1X for Entity Relationship Modelling. In UML notation, the conceptual model is often described with a class diagram in which classes represent concepts, associations represent relationships between concepts and role types of an association represent role types taken by instances of the modelled concepts in various situations. In ER notation, the conceptual model is described with an ER Diagram in which entities represent concepts, cardinality and optionality represent relationships between concepts. Regardless of the notation used, it is important not to compromise the richness and clarity of the business meaning depicted in the conceptual model by expressing it directly in a form influenced by design or implementation concerns.




image




image




image




image




Entity Framework model metadata

image




image




image



Approaches to model creation

VS will create Classes based on Conceptual model of entity framework.  Our application Business objects will contact these classes to persist data into DB.


image




image



Conceptual model in your Apps


image



Demo of Entity framework operation in VS


image




image



image




image




Creating Database First Entity Data Models


image



image




image




image




image




image




image



image




image



image




Mapping to the Database


image



image




image



Modifying an Entity’s Property


image



image



Exploring Relationships


image


Pluralisation will name referenced entities based on Quantity  of records and multiplicity

image



Self referencing Associations


image



image




Many to Many Relationship


This feature is hidden in Entity framework by joining entities during query process.
image



Looking at the model Raw data


image




image




image



image



Property of entity properties


image



Making modification to Conceptual model


image



image



image




image



image




Updating model from DB


image



image




image




Model-First Entity Data Models


image




image



Creating a model from Scratch


image




image




image




image




image




image




image




image




image



Entity Properties


image



image


Store Generated Pattern property

image

Note:  This property will be used only in DB Generated scripts not in Model of entity




image




image



image



Defining Relationship between entities


image




image



image



image



image




image



image



Generating DB schema from the model


image




image




image



image




image



Executing DDL (SQL Scripts) to build the Database


image



image



image



Model and DB cascade Delete


image



image



image



image



Entity Designer DB Generation Power Pack


image


Note:  This tool will help to generate DB scheme for specific object changes not completely




Entity Framework Code First Migrations


Introduction and Automated Migrations

image




image




image




image





What are Migrations?
Migration solves the problem of modifying your database without completely destroying it. And migrations isn't something that was invented by the EF team just to solve Code First's problem. The concept and some tooling already existed, and the idea is to migrate the database, in other words, automatically updated schema to reflect model changes. This way you don't have to lose data or other changes you've made to the database, but you still have the convenience of automatically aligning the database schema to the current version of the model

image




Default Behavior with New Databases

image




image




image



image




image



image



image


image




Default Behavior when Model Changes

Let me take that one step further. I'll going to domain classes. I'll add in a new property, and I'll run the app again. And here is a somewhat familiar exception. This is something you would have seen with Entity Framework 4.1 and 4.2 also when you change the model. Because the default initializer is create database if not exists, it sees that the database already exists; and somehow it's able to tell that the model's changed since the last time we created the database. But it's doing that without the EDM metadata file. So let me just show you a little secret and that is, instead of the EDM metadata table which normally would have shown up alongside aliases and Tweets, Entity Framework 4.3 creates a migration history table. So in the background it's doing the same type of check that it was doing in 4.1 and 4.2. But this time, instead of looking for the EDM metadata table, it does that against migration history. So Entity Framework looked at the representation of what the model was when I originally created this database.
image




image


That representation was stored in migration history. That's how it was able to detect that the model is different than it was before. This is tucked away in system tables. It won't get in your way. And for some people, had I not just shown that to you, you may never have even known that it was there and you might not even care. It just works. The exception message is different. Before it would have told you that you should consider using drop create database if model changes as your initializer; or it even suggested that you might just want to go and manually interact with the database, so make the changes directly to the database. So we're not going to do either of those. With Entity Framework 4.3, it's saying, please use Code First migrations because we worked really hard on them to make you happy. So I'll go ahead and do that.

image




Enabling Code First Migration (And Automating)

image



image



image



image



image




image



Setting the Database Iniitializer to use Migrations

Either use below Code based initializer
image



Or we can use below configuration section to set the initialization and RUN the application.

image



When we execute the application, new property which we changed has added in the DB.
image



image



image



image



image



Removing columns in our model class on Automatic migrations

image



image



image



Adding new entity in the context again

image



image



image



So you might be adding other types of objects to your database. Even though you're working with Code First, you might have triggers in it, stored procedures and other types of objects, and you don't have to worry about losing those or having to re-create them.

image




Migrations with Existing Databases

image




image



image



image



image



image



image



image



image



image



image



image



image



image



image



image



image



image




image




Code-Based Migrations (Manual Migrations)


image




image




image



image



image



image




image



image




image




image




image



image



Update Database parameter options


image



image

Or

image






image



Rolling back with targeted migrations

image


image



image



Data motion and executing SQL from Migration

image



image



image




image




image



image



image




Creating Script for Production Database


image



image



Turning off the Migration and initialization for production

Code level modification to disable

image


Or


Configuration level disablement

image




image



image



image




image






Blogger Labels: Framework,Data,Models,Note,domain,concepts,entities,relationships,Conceptual,computer,science,implementation,example,storage,experts,interpretations,stakeholders,solution,Once,basis,development,generation,realization,domains,platform,notations,Relationship,notation,diagram,role,association,instances,situations,Approaches,creation,Classes,Apps,Demo,Database,Pluralisation,Self,Associations,Many,properties,modification,Model,Scratch,Pattern,scripts,schema,Delete,Designer,Power,Pack,tool,Code,Migrations,Introduction,Migration,team,concept,convenience,version,Default,Behavior,Databases,Changes,exception,aliases,Tweets,history,representation,system,message,Iniitializer,Either,configuration,columns,Automatic,context,procedures,Manual,Update,parameter,options,Script,Production,disablement,metadata,initializer,initialization

No comments:

Post a Comment