The feature of Code First for Entity Framework will allow developer to write code to manage data in database without first creating the table in the database. Although it is helpful in some case, it may also cause confusion if you are not sure what actually happened behind the door.
After the Entity Framework is added into the project, and the models are defined. You will need to create a dbContext class to define the dataset to expose the model as below
[Table("Books")] // Table name
public class Book
{
[Key] // Primary key
public int BookID { get; set; }
public string BookName { get; set; }
public string ISBN { get; set; }
public float price { get; set; }
}
public class BookDbContext :DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Review> Reviews { get; set; }
}.
That is all required for Entity Framework to automatically create the database table with the proper schema. Note you do not need to define the connection string for the database, Visual Studio will automatically select the database based on the following rules:
To verify this, run command in Package Manager Console
Enable-Migrations
add-migration init
update-database
And then connecting to SQLExpress or localDB's database WebApplication1.BookDbContext, you will see the table scheme is automatically created.
At this moment, you can also update configuration seed method to create the initial data, once the code is added, just run "update-database" again will populate the record in the table.
At anytime, if you change the model class public property (scheme), you can run the below command again to update the database. Note seed method is run with the newschema update, so if you do not want to use the seed method to add the initial record again, then you will need commenting out the code in seeding method
add-migration myadditionchange
update-database
In certain cases, the database may already exist, and you only need to use code first to create the model table, if so, you will need to add a connectionstring in web.config under configuration section
<connectionStrings>
After the Entity Framework is added into the project, and the models are defined. You will need to create a dbContext class to define the dataset to expose the model as below
[Table("Books")] // Table name
public class Book
{
[Key] // Primary key
public int BookID { get; set; }
public string BookName { get; set; }
public string ISBN { get; set; }
public float price { get; set; }
}
public class BookDbContext :DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Review> Reviews { get; set; }
}.
That is all required for Entity Framework to automatically create the database table with the proper schema. Note you do not need to define the connection string for the database, Visual Studio will automatically select the database based on the following rules:
- If a local SQL Express instance is available (installed by default with Visual Studio 2010) then Code First has created the database on that instance
- If SQL Express isn’t available then Code First will try and use LocalDb (installed by default with Visual Studio 2012)
- The database is named after the fully qualified name of the derived context, in our case that is WebApplication1.BookDbContext
To verify this, run command in Package Manager Console
Enable-Migrations
add-migration init
update-database
And then connecting to SQLExpress or localDB's database WebApplication1.BookDbContext, you will see the table scheme is automatically created.
At this moment, you can also update configuration seed method to create the initial data, once the code is added, just run "update-database" again will populate the record in the table.
At anytime, if you change the model class public property (scheme), you can run the below command again to update the database. Note seed method is run with the newschema update, so if you do not want to use the seed method to add the initial record again, then you will need commenting out the code in seeding method
add-migration myadditionchange
update-database
In certain cases, the database may already exist, and you only need to use code first to create the model table, if so, you will need to add a connectionstring in web.config under configuration section
<connectionStrings>
<add name="BookContext" connectionString="Data Source=JOHN-PC\SQLEXPRESS;Initial Catalog=WebApplication1.BookDbContext;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
The name is used by contextdb's base class constructor to find the right connection string to use.
public BlogDbContext() : base("name=BookContext")
{
}
Note if you get anything wrong during migration, you can always use
enable-migrations -force
to reset the migration.
No comments:
Post a Comment