Skip to content

Commit

Permalink
Replace Northwind database with scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lewischeng-ms committed May 5, 2015
1 parent d046f92 commit 6f4e16c
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ _[Ss]cripts
*.psess
*.orig
*.pdb
TrippinDB.mdf
TrippinDB_log.ldf
*.mdf
*.ldf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="App_Data\Northwind.mdf" />
<Content Include="App_Data\Northwind_log.ldf">
<DependentUpon>Northwind.mdf</DependentUpon>
<Content Include="App_Data\instnwdb.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Global.asax" />
<Content Include="Web.config">
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,13 @@ public IHttpActionResult IncreasePrice([FromODataUri] int key, ODataActionParame
}
return StatusCode(HttpStatusCode.NoContent);
}

[HttpPost]
[ODataRoute("ResetDataSource")]
public IHttpActionResult ResetDataSource()
{
DbContext.ResetDataSource();
return StatusCode(HttpStatusCode.NoContent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="App_Data\Northwind.mdf" />
<Content Include="App_Data\Northwind_log.ldf">
<DependentUpon>Northwind.mdf</DependentUpon>
<Content Include="App_Data\instnwdb.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Global.asax" />
<Content Include="Web.config" />
Expand Down Expand Up @@ -136,6 +135,7 @@
<Compile Include="Models\Sales_by_Category.cs" />
<Compile Include="Models\Sales_Totals_by_Amount.cs" />
<Compile Include="Models\Shipper.cs" />
<Compile Include="Models\SqlLoader.cs" />
<Compile Include="Models\Summary_of_Sales_by_Quarter.cs" />
<Compile Include="Models\Summary_of_Sales_by_Year.cs" />
<Compile Include="Models\Supplier.cs" />
Expand All @@ -152,7 +152,6 @@
<DependentUpon>Web.config</DependentUpon>
</None>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\Microsoft.Restier.Conventions\Microsoft.Restier.Conventions.csproj">
<Project>{5a10c8bb-5ca0-4d41-a731-45cd29a30d34}</Project>
Expand Down
24 changes: 24 additions & 0 deletions src/Microsoft.Restier.Samples.Northwind/Models/NorthwindContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ protected override void OnConfiguring(DbContextOptions options)
public NorthwindContext()
: base("name=NorthwindConnection")
{
if (!Database.Exists())
{
LoadDataSource();
}
}
#endif

Expand Down Expand Up @@ -64,6 +68,26 @@ public NorthwindContext()
public virtual DbSet<Summary_of_Sales_by_Quarter> Summary_of_Sales_by_Quarters { get; set; }
public virtual DbSet<Summary_of_Sales_by_Year> Summary_of_Sales_by_Years { get; set; }

public void ResetDataSource()
{
if (Database.Exists())
{
Database.Delete();
}

LoadDataSource();
}

private static void LoadDataSource()
{
var dbPath = SqlLoader.GetDatabaseDirectory(null);
var loader = new SqlLoader();
loader.SetDatabaseEngine("(localdb)\\v11.0");
loader.AddScript("instnwdb.sql");
loader.AddScriptArgument("SqlSamplesDatabasePath", dbPath);
loader.Execute(dbPath);
}

#if EF7
protected override void OnModelCreating(Data.Entity.Metadata.ModelBuilder modelBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Microsoft.Restier.Samples.Northwind.Models
[Grant(DomainPermissionType.All, On = "Regions")]
[Grant(DomainPermissionType.Inspect, On = "Suppliers")]
[Grant(DomainPermissionType.Read, On = "Suppliers")]
[Grant(DomainPermissionType.All, On = "ResetDataSource")]
public class NorthwindDomain : DbDomain<NorthwindContext>
{
public NorthwindContext Context { get { return DbContext; } }
Expand All @@ -42,6 +43,11 @@ protected EdmModel OnModelExtending(EdmModel model)
increasePrice.AddParameter("bindingParameter", new EdmEntityTypeReference(product as IEdmEntityType, false));
increasePrice.AddParameter("diff", EdmCoreModel.Instance.GetInt32(false));
model.AddElement(increasePrice);

var resetDataSource = new EdmAction(ns, "ResetDataSource", null, false, null);
model.AddElement(resetDataSource);
var entityContainer = (EdmEntityContainer)model.EntityContainer;
entityContainer.AddActionImport("ResetDataSource", resetDataSource);
return model;
}

Expand Down
95 changes: 95 additions & 0 deletions src/Microsoft.Restier.Samples.Northwind/Models/SqlLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Microsoft.Restier.Samples.Northwind.Models
{
internal class SqlLoader
{
private static readonly string[] possibleSqlCmdExePaths =
{
@"%ProgramFiles%\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE",
@"%ProgramW6432%\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE",
@"%ProgramFiles%\Microsoft SQL Server\120\Tools\Binn\SQLCMD.EXE",
@"%ProgramW6432%\Microsoft SQL Server\120\Tools\Binn\SQLCMD.EXE"
};

private readonly IList<KeyValuePair<string, string>> _options = new List<KeyValuePair<string, string>>();

public void SetDatabaseEngine(string dbName)
{
AddOption("-S", dbName);
}

public void AddScriptArgument(string argName, string argValue)
{
AddOption("-v", string.Format("{0}=\"{1}\"", argName, argValue));
}

public void AddScript(string scriptName)
{
AddOption("-i", scriptName);
}

public void Reset()
{
_options.Clear();
}

public void Execute(string workingDirectory)
{
var sqlCmdExePath = possibleSqlCmdExePaths.Select(Environment.ExpandEnvironmentVariables).First(File.Exists);

var argumentStringBuilder = new StringBuilder();
foreach (var option in _options)
{
argumentStringBuilder.Append(string.Format("{0} {1} ", option.Key, option.Value));
}

var start = new ProcessStartInfo()
{
FileName = sqlCmdExePath,
WorkingDirectory = workingDirectory,
Arguments = argumentStringBuilder.ToString(),
UseShellExecute = false,
CreateNoWindow = true
};

Process.Start(start).WaitForExit();
}

public static string GetDatabaseDirectory(string subDir)
{
var dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory") as string;
if (string.IsNullOrEmpty(dataDirectory))
{
dataDirectory = Environment.CurrentDirectory;
}

if (!string.IsNullOrEmpty(subDir))
{
dataDirectory = Path.Combine(dataDirectory, subDir);
}

return AddTrailingSlash(dataDirectory);
}

private void AddOption(string key, string value)
{
_options.Add(new KeyValuePair<string, string>(key, value));
}

private static string AddTrailingSlash(string path)
{
if (path.EndsWith("\\"))
{
return path;
}

return path + "\\";
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@
<Content Include="..\Microsoft.Restier.Samples.Northwind.Tests\Baselines\TestPutProductReturnContent.txt">
<Link>Baselines\TestPutProductReturnContent.txt</Link>
</Content>
<Content Include="App_Data\Northwind.mdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="App_Data\Northwind_log.ldf">
<Content Include="App_Data\instnwdb.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="App_Data\Northwind.mdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="App_Data\Northwind_log.ldf">
<Content Include="App_Data\instnwdb.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Baselines\TestBatch.txt">
Expand Down

0 comments on commit 6f4e16c

Please sign in to comment.