<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Entity_Framework_7</id>
		<title>Entity Framework 7 - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Entity_Framework_7"/>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Entity_Framework_7&amp;action=history"/>
		<updated>2026-06-02T10:38:02Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>https://logicwiki.co.uk/index.php?title=Entity_Framework_7&amp;diff=273&amp;oldid=prev</id>
		<title>Dt1nh6: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Entity_Framework_7&amp;diff=273&amp;oldid=prev"/>
				<updated>2016-05-09T13:27:29Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;tr style='vertical-align: top;' lang='en'&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 13:27, 9 May 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan='2' style='text-align: center;' lang='en'&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Dt1nh6</name></author>	</entry>

	<entry>
		<id>https://logicwiki.co.uk/index.php?title=Entity_Framework_7&amp;diff=272&amp;oldid=prev</id>
		<title>Macrop at 10:08, 2 February 2016</title>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Entity_Framework_7&amp;diff=272&amp;oldid=prev"/>
				<updated>2016-02-02T10:08:29Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Entity Framework]]&lt;br /&gt;
[[Category:ASP.NET5]]&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
 PM&amp;gt; Install-Package EntityFramework.MicrosoftSqlServer -Pre&lt;br /&gt;
&lt;br /&gt;
== Creating Models ==&lt;br /&gt;
=== Models ===&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
public class Parent&lt;br /&gt;
{&lt;br /&gt;
  public int Id {get; set;}&lt;br /&gt;
  public string MasterName {get; set;}&lt;br /&gt;
  public ICollection&amp;lt;Child&amp;gt; {get; set;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Child&lt;br /&gt;
{&lt;br /&gt;
  public int Id {get; set;}&lt;br /&gt;
  public string ChildName {get; set;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Context ===&lt;br /&gt;
Then we start creating context. Create a **TestContext.cs** file in Models folder&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
public class TestContext : DbContext&lt;br /&gt;
{&lt;br /&gt;
  public DbSet&amp;lt;Parent&amp;gt; Parents {get; set;}&lt;br /&gt;
  public DbSet&amp;lt;Child&amp;gt; Children {get; set;}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
== Configure Services ==&lt;br /&gt;
in **Startup.cs** under **ConfigureServices** section add the lines below&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
services.AddEntityFramework()&lt;br /&gt;
  .AddSqlServer()&lt;br /&gt;
  .AddDbContext&amp;lt;TestContext&amp;gt;();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
**TestContext** here is the one we defined in Context section &lt;br /&gt;
&lt;br /&gt;
== Configure Db Provider ==&lt;br /&gt;
in config.json add a new section named Data &lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot;Data&amp;quot;:{&amp;quot;TestContextConnection&amp;quot;:&amp;quot;Server=SomeServer;&lt;br /&gt;
    Database=TestDb;&lt;br /&gt;
    Trusted_Connection=true;&lt;br /&gt;
    MultipleActiveResultSets=true;&amp;quot;}&lt;br /&gt;
    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in **TestContext.cs** file again &lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
protected override void OnConfiguring(DbContext TestContext, OptionsBuilder optionsBuilder)&lt;br /&gt;
{&lt;br /&gt;
  var connString = Startup.Configuration[&amp;quot;Data:TestContextConnection&amp;quot;]&lt;br /&gt;
  optionsBuilder.UseSqlServer(connString)&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Migrations ==&lt;br /&gt;
in **project.json** file add a dependency and add a line to **commands** section&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;quot;EntityFramwework.Commands&amp;quot;, &amp;quot;7.0.0-beta8&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
  // in commands section &lt;br /&gt;
  &amp;quot;ef&amp;quot;:&amp;quot;EntityFramework.Commands&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then in Command Prompt under the project folder&lt;br /&gt;
&amp;lt;sxh&amp;gt;&lt;br /&gt;
dnx ef migrations add InitialDatabase&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
**ef** comes from project.json commands section and InitialDatabase is a name that we gave to this migration. &lt;br /&gt;
&lt;br /&gt;
in **TestContext.cs** file &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
public class TestContext:DbContext&lt;br /&gt;
{&lt;br /&gt;
  //in constructor add the line below&lt;br /&gt;
  public TextContext() &lt;br /&gt;
  {&lt;br /&gt;
    Database.EnsureCreated();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will create the database itself and if any migrations nedd to be executed it will execute them at the same time.&lt;br /&gt;
&lt;br /&gt;
== Seeding Data ==&lt;br /&gt;
Create **TestContextSeedData.cs** file in models folder&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
public class TestContextSeedData&lt;br /&gt;
{&lt;br /&gt;
  private TestContext = _context;&lt;br /&gt;
  public TestContextSeedData(TestContext context)&lt;br /&gt;
  {&lt;br /&gt;
    _context = context&lt;br /&gt;
  }&lt;br /&gt;
    public void EnsureSeedData()&lt;br /&gt;
    {&lt;br /&gt;
      if(! _context.Masters.Any())&lt;br /&gt;
      {&lt;br /&gt;
        var rMaster = new Master()&lt;br /&gt;
        {&lt;br /&gt;
          MasterName = &amp;quot;test&amp;quot;,&lt;br /&gt;
          Children = new List&amp;lt;Child&amp;gt;()&lt;br /&gt;
          {&lt;br /&gt;
            new Child(){ ChildName = &amp;quot;TestChild&amp;quot;},&lt;br /&gt;
            new Child(){ ChildName = &amp;quot;TestChild2&amp;quot;}            &lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
        _context.Masters.Add(rMaster);&lt;br /&gt;
        _context.Children.AddRange(rMaster.Children);&lt;br /&gt;
        _context.SaveChanges();&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in startup.cs under Configure method &lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
public void Configure(TestContextSeedData seeder)&lt;br /&gt;
{&lt;br /&gt;
 seeder.EnsureSeedData();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use this class we need to register it in **startup.cs** file in **ConfigureServices**&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:csharp;&amp;quot;&amp;gt;&lt;br /&gt;
services.AddTransient&amp;lt;TestContentSeedData&amp;gt;();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Options ===&lt;br /&gt;
  * AddTransient : only one instance supplied&lt;br /&gt;
  * AddSingleton : &lt;br /&gt;
  * AddInstance : &lt;br /&gt;
  * AddScoped : an instance will be created whenever needed&lt;br /&gt;
&lt;br /&gt;
Stop and restart web server to activate services changes&lt;/div&gt;</summary>
		<author><name>Macrop</name></author>	</entry>

	</feed>