Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
Use ErikEJ.EntityFrameworkCore.SqlServer.Dacpac
Browse files Browse the repository at this point in the history
Resolves #4
  • Loading branch information
bricelam committed Feb 4, 2020
1 parent 1f49090 commit c6cbb7f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ChinookApp/ChinookApp.csproj
Expand Up @@ -10,7 +10,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
<PackageReference Include="ErikEJ.EntityFrameworkCore.SqlServer.Dacpac" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion ChinookApp/Data/ChinookDatabaseContext.cs
Expand Up @@ -18,7 +18,7 @@ public partial class ChinookDatabaseContext : DbContext
public virtual DbSet<Track> Tracks { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("Data Source=(localdb)\\ProjectsV13;Initial Catalog=ChinookDatabase");
=> options.UseSqlServer("Data Source=(local);Initial Catalog=ChinookDatabase");

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down
7 changes: 3 additions & 4 deletions ChinookDatabase/ChinookDatabase.sqlproj
Expand Up @@ -3,16 +3,16 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<Name>Chinook</Name>
<Name>ChinookDatabase</Name>
<SchemaVersion>2.0</SchemaVersion>
<ProjectVersion>4.1</ProjectVersion>
<ProjectGuid>{ccddcd07-72d2-452c-b0e7-56db5a12f1eb}</ProjectGuid>
<DSP>Microsoft.Data.Tools.Schema.Sql.Sql130DatabaseSchemaProvider</DSP>
<OutputType>Database</OutputType>
<RootPath>
</RootPath>
<RootNamespace>Chinook</RootNamespace>
<AssemblyName>Chinook</AssemblyName>
<RootNamespace>ChinookDatabase</RootNamespace>
<AssemblyName>ChinookDatabase</AssemblyName>
<ModelCollation>1033, CI</ModelCollation>
<DefaultFileStructure>BySchemaType</DefaultFileStructure>
<DeployToDatabase>True</DeployToDatabase>
Expand All @@ -22,7 +22,6 @@
<SqlServerVerification>False</SqlServerVerification>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseSet>True</TargetDatabaseSet>
<DacApplicationName>Chinook</DacApplicationName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
Expand Down
10 changes: 9 additions & 1 deletion EFCore.TextTemplating/MyModelGenerator.cs
Expand Up @@ -34,6 +34,14 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO
{
var resultingFiles = new ScaffoldedModel();

var connectionString = options.ConnectionString;

// HACK: Work around dotnet/efcore#19799
if (File.Exists(connectionString))
{
connectionString = "Data Source=(local);Initial Catalog=" + (string)model["Scaffolding:DatabaseName"];
}

var contextGenerator = new MyDbContextGenerator
{
Session = new Dictionary<string, object>
Expand All @@ -42,7 +50,7 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO
["ModelNamespace"] = options.ModelNamespace,
["Namespace"] = options.ContextNamespace,
["ContextName"] = options.ContextName,
["ConnectionString"] = options.ConnectionString,
["ConnectionString"] = connectionString,
["SuppressConnectionStringWarning"] = options.SuppressConnectionStringWarning,
["UseDataAnnotations"] = options.UseDataAnnotations,

Expand Down
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -17,7 +17,7 @@ ChinookApp references EFCore.TextTemplating and contains an assembly-level attri
"EFCore.TextTemplating.DesignTimeServices, EFCore.TextTemplating")]
```

ChinookApp also references the EF Core tools and [my pluralizer extension](https://github.com/bricelam/EFCore.Pluralizer) to enhance the scaffolded code.
ChinookApp also references the EF Core tools and [my pluralizer extension](https://github.com/bricelam/EFCore.Pluralizer) to enhance the scaffolded code and ErikEJ.EntityFrameworkCore.SqlServer.Dacpac from the [EF Core Power Tools](https://github.com/ErikEJ/EFCorePowerTools) to reverese engineer directly from a DACPAC.

EFCore.TextTemplating contains three templates: one for scaffolding [the DbContext](EFCore.TextTemplating/MyDbContextGenerator.tt), one for [the IEntityTypeConfiguration](EFCore.TextTemplating/MyEntityTypeConfigurationGenerator.tt) implementations, and one for [the entity types](EFCore.TextTemplating/MyEntityTypeGenerator.tt).

Expand All @@ -38,17 +38,17 @@ Reverse engineering the model can be done in [the normal way](https://docs.micro

```sh
dotnet ef dbcontext scaffold \
"Data Source=(localdb)\ProjectsV13;Initial Catalog=ChinookDatabase" \
Microsoft.EntityFrameworkCore.SqlServer \
../ChinookDatabase/bin/Debug/ChinookDatabase.dacpac \
ErikEJ.EntityFrameworkCore.SqlServer.Dacpac \
--output-dir Models \
--context-dir Data \
--force
```
or
```ps1
Scaffold-DbContext `
"Data Source=(localdb)\ProjectsV13;Initial Catalog=ChinookDatabase" `
Microsoft.EntityFrameworkCore.SqlServer `
..\ChinookDatabase\bin\Debug\ChinookDatabase.dacpac `
ErikEJ.EntityFrameworkCore.SqlServer.Dacpac `
-OutputDir Models `
-ContextDir Data `
-Force
Expand Down

0 comments on commit c6cbb7f

Please sign in to comment.