Controlling the database

Image

In the next chapter we’ll look at two methods for controlling the database schema that Entity Framework creates based on your class structure, but one of the most basic conventions (and one you’ll almost certainly want to use) is how, when and where the database is created. Let’s start by changing the name of the database. After all, CodeFirstDataAccess.RecipeContext is almost certainly not what you want!

You can control the name of the database by adding a connection string in the application’s configuration file (we’ll look at that in a minute), but if all you need to do is change the name of the database, the easiest way is to just pass the name you want to the context constructor:


Image

public class RecipeContext : DbContext
{
 public DbSet<> Recipes {get; set;}

 public RecipeContext()
  : base("CodeFirstRecipes")
 {}
}



Image

Public Class RecipeContext
    Inherits DbContext

    Public Property Recipes As DbSet(Of Recipe)

    Public Sub New()
        MyBase.New("CodeFirstRecipes")
    End Sub
End Class


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset