Entity framework basic guideline

1. Virtual keyword in class attribute
public virtual ICollection<Enrollment> Enrollments { get; set; }
virtual so that they can take advantage of certain Entity Framework functionality such as lazy loading. (Lazy loading will be explained later, in the Reading Related Data tutorial later in this series.)


can hold multiple entities (as in many-to-many or one-to-many relationships), its type must be a list in which entries can be added, deleted, and updated, such as ICollection.


2. Nullable
public Grade? Grade { get; set; }
The Grade property is an enum. The question mark after the Grade type declaration indicates that the Grade property is nullable. A grade that's null is different from a zero grade — null means a grade isn't known or hasn't been assigned yet.


3. [DatabaseGenerated(DatabaseGeneratedOption.None)]
       public int CourseID { get; set; }
he DatabaseGenerated attribute in a later tutorial in this series. Basically, this attribute lets you enter the primary key for the course rather than having the database generate it.


4. Database Context class
The main class that coordinates Entity Framework functionality for a given data model is the database context class.You create this class by deriving from the System.Data.Entity.DbContext class.


5. Entity Sets
This code creates a DbSet property for each entity set. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.


public DbSet Students { get; set; }


6. Specifying connection string


public class SchoolContext : DbContext
   {


//ou could also pass in the connection string itself instead of the name of one that is stored in the Web.config file.
       public SchoolContext() : base("SchoolContext")
       {
       }


       public DbSet Students { get; set; }
       public DbSet Enrollments { get; set; }
       public DbSet Courses { get; set; }

//specifies the singular table name . The modelBuilder.Conventions.Remove statement in the OnModelCreating method prevents table names from being pluralized.
If you didn't do this, the generated tables in the database would be named Students, Courses, and Enrollments.


       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Conventions.Remove();
       }
   }

7. Initialize the database with test data
The Entity Framework can automatically create (or drop and re-create) a database for you when the application runs. You can specify that this should be done every time your application runs or only when the model is out of sync with the existing database. You can also write a Seedmethod that the Entity Framework automatically calls after creating the database in order to populate it with test data.


8. Seed method
Seed method that the Entity Framework automatically calls after creating the database in order to populate it with test data.


9. Setup for drop and create database when model chages or database already exists
The default behavior is to create a database only if it doesn't exist (and throw an exception if the model has changed and the database already exists). In this section you'll specify that the database should be dropped and re-created whenever the model changes


public class SchoolInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges
   {


   }


10. Tell the Entity framework to use SchoolInitializer class : configure in web.config



   connection-type="ContosoUniversity.DAL.SchoolContext, University-project">
      databaseinitializer-type="ContosoUniversity.DAL.SchoolInitializer, University-project" />
   

 
..


  • The context type specifies the fully qualified context class name and the assembly it's in
  • databaseinitializer type specifies the fully qualified name of the initializer class and the assembly it's in
  • As an alternative to setting the initializer in the Web.config file is to do it in code by adding a Database.SetInitializer statement to theApplication_Start method in the Global.asax.cs file.


11. Setup EF to use local SQL server
   

Comments