EF 4.1 Code First Database re-create

 

If you’ve upgraded to Entity Framework 4.1 you may have noticed the following no longer compiles. (the line of code that causes the database to drop and recreate on schema change).

image

Solution

DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<TCF.Models.TCFContext>());

 

image

Asp MVC Entity Framework and Data annotations

 

image

 

When you create a MVC view for data entry it already has the scaffolding in place to handle client side validation (and server)
However you need to specify the data annotations you need.

You’ve two options.

1) Modify the T4 code generation templates

2) Use meta data classes (this post)

The generated EF classes are partial classes, this allows us to do the following

   1:  #region Feed
   2:   
   3:      [MetadataType(typeof(FeedMetadata))]
   4:      public partial class Feed
   5:      {
   6:      }
   7:   
   8:   
   9:      public class FeedMetadata
  10:      {
  11:          [Required(ErrorMessage="Please enter a valid name for this feed")]
  12:          public string Name { get; set; }
  13:      }
  14:   
  15:      #endregion Feeds

 

Note: while mvc3 supports these annotations they are limited to [range/required/stringlength/regularexpression]. If you wish to have a bigger offering consider the extensions offered.

Here’s how.

Go the NuGet Extension Manager (right click on references and select add Library Package Reference) and search for “DataAnnotationsExtensions.”  You should see the following two packages, choose the second (includes client side validation, first is server side only.)

Btw: If NuGet is alien to you, then don’t worry, it’s basically a packet manager to make your life a littler easier… a post for another day!

Recent Tweets

Note: For Customization and Configuration, CheckOut Recent Tweets Documentation

Calendar

<<  September 2017  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

View posts in large calendar

Month List