Skip to content

Instantly share code, notes, and snippets.

@rmja
Forked from natemcmaster/MyContext.cs
Last active August 29, 2015 14:23
Show Gist options
  • Save rmja/691506a58907c07f9ce2 to your computer and use it in GitHub Desktop.
Save rmja/691506a58907c07f9ce2 to your computer and use it in GitHub Desktop.
{
"projects": ["c:/source/entityframework/src"]
}
using Microsoft.Data.Entity;
namespace ConsoleApp1
{
public class MyContext : DbContext
{
public DbSet<Address> Addresses { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=localhost\\sqlexpress;Database=rep2057;Trusted_Connection=True;MultipleActiveResultSets=true");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Model.AddEntityType(typeof(CustomerAddress));
modelBuilder.Model.AddEntityType(typeof(EmployeeAddress));
var address = modelBuilder.Entity<Address>();
address.Key(x => x.Id);
address.Metadata.Relational().DiscriminatorProperty = address.Property(x => x.Discriminator).Metadata;
var customer = modelBuilder.Entity<CustomerAddress>().Metadata;
customer.BaseType = modelBuilder.Model.GetEntityType(typeof(Address));
customer.Relational().DiscriminatorValue = Discriminator.CustomerAddress;
var employee = modelBuilder.Entity<EmployeeAddress>().Metadata;
employee.BaseType = modelBuilder.Model.GetEntityType(typeof(Address));
employee.Relational().DiscriminatorValue = Discriminator.EmployeeAddress;
}
}
}
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
namespace ConsoleApp1
{
public class Program
{
public void Main(string[] args)
{
var logger = new SimpleLoggerFactory();
var serviceCollection = new ServiceCollection();
serviceCollection
.AddInstance<ILoggerFactory>(logger)
.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyContext>();
var provider = serviceCollection.BuildServiceProvider();
using (var _db = provider.GetService<MyContext>())
{
// _db.Database.AsRelational().EnsureCreated();
var result = _db.Addresses.Where(x => x.Discriminator == Discriminator.CustomerAddress).ToList();
Console.WriteLine(result.Count);
}
//var sql = logger.Instance.Sb.ToString();
//Console.WriteLine(sql);
//Debug.Assert(sql.Contains("@__p_0: 1"));
//Console.ReadLine();
}
}
public abstract class Address
{
public int Id { get; set; }
public Discriminator Discriminator { get; private set; }
private Address()
{
}
protected Address(Discriminator discriminator)
{
Discriminator = discriminator;
}
}
public class CustomerAddress : Address
{
public CustomerAddress()
: base(Discriminator.CustomerAddress)
{
}
}
public class EmployeeAddress : Address
{
public EmployeeAddress()
: base(Discriminator.EmployeeAddress)
{
}
}
public enum Discriminator
{
CustomerAddress = 1,
EmployeeAddress = 2
}
}
{
"version": "1.0.0-*",
"description": "",
"authors": [ "" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"EntityFramework.SqlServer": "7.0.0-*"
},
"commands": {
"ConsoleApp1" : "ConsoleApp1"
},
"frameworks" : {
"dnx451": { }
}
}

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22823.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "reproduce2057", "reproduce2057.xproj", "{171473B3-CFDC-4743-A0C9-40B0A1388D79}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2EC230E8-C618-4FC4-BA0D-8A1B958B54D5}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "EntityFramework.SqlServer", "..\entityframework\src\EntityFramework.SqlServer\EntityFramework.SqlServer.xproj", "{1ACE01A3-97E2-41A7-A315-4AE53E53794E}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "EntityFramework.Relational", "..\entityframework\src\EntityFramework.Relational\EntityFramework.Relational.xproj", "{AD2D13EB-AA07-45FB-B946-1F61CCC0D9F7}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "EntityFramework.Core", "..\entityframework\src\EntityFramework.Core\EntityFramework.Core.xproj", "{CE8F7D9C-FAC7-4BA9-868F-47A8BA3C381B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{171473B3-CFDC-4743-A0C9-40B0A1388D79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{171473B3-CFDC-4743-A0C9-40B0A1388D79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{171473B3-CFDC-4743-A0C9-40B0A1388D79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{171473B3-CFDC-4743-A0C9-40B0A1388D79}.Release|Any CPU.Build.0 = Release|Any CPU
{1ACE01A3-97E2-41A7-A315-4AE53E53794E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1ACE01A3-97E2-41A7-A315-4AE53E53794E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1ACE01A3-97E2-41A7-A315-4AE53E53794E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1ACE01A3-97E2-41A7-A315-4AE53E53794E}.Release|Any CPU.Build.0 = Release|Any CPU
{AD2D13EB-AA07-45FB-B946-1F61CCC0D9F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD2D13EB-AA07-45FB-B946-1F61CCC0D9F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD2D13EB-AA07-45FB-B946-1F61CCC0D9F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD2D13EB-AA07-45FB-B946-1F61CCC0D9F7}.Release|Any CPU.Build.0 = Release|Any CPU
{CE8F7D9C-FAC7-4BA9-868F-47A8BA3C381B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE8F7D9C-FAC7-4BA9-868F-47A8BA3C381B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE8F7D9C-FAC7-4BA9-868F-47A8BA3C381B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE8F7D9C-FAC7-4BA9-868F-47A8BA3C381B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="__ToolsVersion__" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>171473b3-cfdc-4743-a0c9-40b0a1388d79</ProjectGuid>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
using System;
using System.Text;
using Microsoft.Framework.Logging;
namespace ConsoleApp1
{
public class SimpleLoggerFactory : ILoggerFactory
{
public SimpleLogger Instance { get; } = new SimpleLogger();
public ILogger CreateLogger(string categoryName)
{
return Instance;
}
public void AddProvider(ILoggerProvider provider)
{
}
public LogLevel MinimumLevel { get; set; }
public class SimpleLogger : ILogger
{
public StringBuilder Sb { get; } = new StringBuilder();
public void Log(LogLevel logLevel, int eventId, object state, Exception exception,
Func<object, Exception, string> formatter)
{
if (formatter == null || eventId != 42)
return;
Sb.AppendLine(formatter(state, exception));
}
public bool IsEnabled(LogLevel logLevel) => true;
public IDisposable BeginScopeImpl(object state)
{
return new NullScope();
}
public class NullScope : IDisposable
{
public void Dispose()
{
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment