Example
- Create a console application.
- Install EntityFramework nuget package by running
Install-Package EntityFramework
in "Package Manager Console"
- Add your connection string in app.config file , It's important to include
providerName="System.Data.SqlClient"
in your connection.
- Create a public class as you wish , some thing like "
Blog
"
- Create Your ContextClass which inherit from DbContext , some thing like "
BlogContext
"
- Define a property in your context of DbSet type , some thing like this :
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
}
public class BlogContext: DbContext
{
public BlogContext(): base("name=Your_Connection_Name")
{
}
public virtual DbSet<Blog> Blogs{ get; set; }
}
- It's important to pass the connection name in constructor ( here Your_Connection_Name)
- In Package Manager Console run
Enable-Migration
command , This will create a migration folder in your project
- Run
Add-Migration Your_Arbitrary_Migraiton_Name
command , this will create a migration class in migrations folder with two method Up() and Down()
- Run
Update-Database
command in order to create a database with a blog table