Entity Framework 7
Contents
Installation
PM> Install-Package EntityFramework.MicrosoftSqlServer -Pre
Creating Models
Models
public class Parent
{
public int Id {get; set;}
public string MasterName {get; set;}
public ICollection<Child> {get; set;}
}
public class Child
{
public int Id {get; set;}
public string ChildName {get; set;}
}
Context
Then we start creating context. Create a **TestContext.cs** file in Models folder
public class TestContext : DbContext
{
public DbSet<Parent> Parents {get; set;}
public DbSet<Child> Children {get; set;}
}
Once we write **DbContext** in the class above it will be underlined and as a fix VS suggest to get EF7 Nuget package. Click this suggestion.
Configure Services
in **Startup.cs** under **ConfigureServices** section add the lines below
services.AddEntityFramework() .AddSqlServer() .AddDbContext<TestContext>();
- TestContext** here is the one we defined in Context section
Configure Db Provider
in config.json add a new section named Data
"Data":{"TestContextConnection":"Server=SomeServer;
Database=TestDb;
Trusted_Connection=true;
MultipleActiveResultSets=true;"}
in **TestContext.cs** file again
protected override void OnConfiguring(DbContext TestContext, OptionsBuilder optionsBuilder)
{
var connString = Startup.Configuration["Data:TestContextConnection"]
optionsBuilder.UseSqlServer(connString)
}
Migrations
in **project.json** file add a dependency and add a line to **commands** section
"EntityFramwework.Commands", "7.0.0-beta8" // in commands section "ef":"EntityFramework.Commands"
Then in Command Prompt under the project folder <sxh> dnx ef migrations add InitialDatabase </pre>
- ef** comes from project.json commands section and InitialDatabase is a name that we gave to this migration.
in **TestContext.cs** file
public class TestContext:DbContext
{
//in constructor add the line below
public TextContext()
{
Database.EnsureCreated();
}
}
This will create the database itself and if any migrations nedd to be executed it will execute them at the same time.
Seeding Data
Create **TestContextSeedData.cs** file in models folder
public class TestContextSeedData
{
private TestContext = _context;
public TestContextSeedData(TestContext context)
{
_context = context
}
public void EnsureSeedData()
{
if(! _context.Masters.Any())
{
var rMaster = new Master()
{
MasterName = "test",
Children = new List<Child>()
{
new Child(){ ChildName = "TestChild"},
new Child(){ ChildName = "TestChild2"}
}
}
_context.Masters.Add(rMaster);
_context.Children.AddRange(rMaster.Children);
_context.SaveChanges();
}
}
}
in startup.cs under Configure method
public void Configure(TestContextSeedData seeder)
{
seeder.EnsureSeedData();
}
To use this class we need to register it in **startup.cs** file in **ConfigureServices**
services.AddTransient<TestContentSeedData>();
Options
* AddTransient : only one instance supplied * AddSingleton : * AddInstance : * AddScoped : an instance will be created whenever needed
Stop and restart web server to activate services changes