RcBuilder@walla.com
call centre: 054-5614020

Entity Framework Model First

30
Oct
2014
Posted by: RcBuilder  /   Category: ADO.NET / Entity Framework / SQL / Stored Procedure   /   No Comments

Entity Framework Model First

Create model

* Add -> New Item -> (Data Tab) ADO.NET Entity Data Model (.edmx) -> set mame -> ok

* in the wizard -> choose ‘Empty Model’ option

* by right clicking within the edmx area we can choose which elements to add to our model
- Entity : add a new POCO object (Book, User etc.)

* set Entity Name, Key Name and Type and parent class if needed – ok

* right click on the ‘Properties’ tab in the created Entity we created above -> add new (see ‘Properties Types’)

* use the properties window (F4) in order to set/update the features of the current property (property Type, Default Value etc.)
- Enum Type : add a new enum collection

* set the enum name, add names and values to the collection (values are optional)

* in order to add enum type as a property of an entity we’ll use a Scalar type property and choose the enum as the property type
- Association : add a relation between entites (1 to 1, 1 to many or many to many)

* set the association name (usually a concatenate of both entities – StoreBooks)

* set the actual association (many books in store, single store each book etc.)
tip: we can choose whether to add navigation property or not
tip: we have a verbal version that describes the current association
notice! we can manage all our added data (Entities, Properties etc.) in the Model Browser window

* we can generate database based on our model (see ‘Generate Database’)

Properties Types

* Scalar – any primitive or POCO object
* Complex – a non scalar property, usually a complex property is a collection of scalar properties
* Navigation

Generate Database

after we defined our model, we can generate a database based on this model we’ve just created.
right click within the edmx area -> generate database from model -> set connection string or choose from the list -> ok
the .Net will then create a sql script that we can execute on our sql management or directly from the visual studio,
this script will create our model as defined in the edmx file

notice! for any change in the model structure we should perform another generate action,
and any generation drops and creates the whole database
Database connector

when using the ‘Model First’ approach to generate our database,
in addition to all the generated code, the EF will create a database connector for us (inherit from DbContext)
so we could work with our model the very same as we work in the ‘Database First’ approach later on
USING

public void WorkingWithTheEntities() {
var store = new Store() { Address = “somewhere over the rainbow … ” };
store.Books.Add(new Book() { Name = “BOOK 10″, NumOfPages = 30, Price = 35, Publisher = ePublisher.Keter });
store.Books.Add(new Book() { Name = “BOOK 20″, NumOfPages = 45, Price = 45, Publisher = ePublisher.Opus });
store.Books.Add(new Book() { Name = “BOOK 30″, NumOfPages = 310, Price = 135, Publisher = ePublisher.Modan });
store.Books.Add(new Book() { Name = “BOOK 40″, NumOfPages = 103, Price = 56, Publisher = ePublisher.Opus });

foreach (var b in store.Books)
Console.WriteLine(b.Name);
}

public void WorkingWithTheDatabase()
{
using (var context = new SomeEmptyModelContext())
{
var store = new Store() { Address = “somewhere over the rainbow … ” };
store.Books.Add(new Book() { Name = “BOOK 10″, NumOfPages = 30, Price = 35, Publisher = ePublisher.Keter });
store.Books.Add(new Book() { Name = “BOOK 20″, NumOfPages = 45, Price = 45, Publisher = ePublisher.Opus });
store.Books.Add(new Book() { Name = “BOOK 30″, NumOfPages = 310, Price = 135, Publisher = ePublisher.Modan });
store.Books.Add(new Book() { Name = “BOOK 40″, NumOfPages = 103, Price = 56, Publisher = ePublisher.Opus });

context.Stores.Add(store); // add store
context.SaveChanges(); // update database
}
}

Author Avatar

About the Author

בניית אתרים ופתרונות טכנולוגים | RcBuilder

No Comments


  • פיתוח מערכות
  • פתרונות טכנולוגים
  • קידום אתרים
  • בניית אתרים