Pull down the SQL Server Docker Image from DockerHub docker pull microsoft/mssql-server-linux:2017-latest
Create a Docker container using the microsoft/mssql-server-linux:2017-latest Automatically accept the EULA, set the password to for the sa user, and map port container port 1433 to host port 1433 docker run --name sql \ -e 'ACCEPT_EULA=Y' \ -e 'SA_PASSWORD=' \ -p 1433:1433 \ -d microsoft/mssql-server-linux:2017-latest
Check if the container is up (running) – in STATUS column sudo docker ps -a
To stop the container (assuming its name is “sql”) just type docker stop sql Now, typing sudo docker ps -a the container STATUS is Exited
To restart the container just type docker start sql
Generating a model from an existing database .Net Core 2.0
In a folder that will contain your demo project type the following command: dotnet new console for a console application or dotnet new mvc for a MVC or Web API application
Add the Entity Framework Core and Tools packages to the project: This is the EF Core provider for SQL Server. dotnet add package Microsoft.EntityFrameworkCore.SqlServer
This package contains the Entity Framework Core commands. Both of these packages are required for any Entity Framework Core application that targets SQL Server. dotnet add package Microsoft.EntityFrameworkCore.Tools
The final package is required for supporting the scaffolding of the model. dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design
Test if Entity Framework is available dotnet ef -h
Now use the DbContext Scaffold command to generate the model passing two arguments:
a connection string
a provider
dotnet ef dbcontext scaffold "Server=.\;Database=AdventureWorks;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
dotnet ef dbcontext scaffold "Server=localhost,1433\\Catalog=AdventureWorks;Database=AdventureWorks;User=SA;Password=;" Microsoft.EntityFrameworkCore.SqlServer -o Models
Once you have executed the command, you will see that a folder named Models has been created in the project folder, containing a collection of class files representing the entities. You can find also a class file containing the DbContext class. The DbContext class will take the name of the database plus “Context”. You can override this using the -c or –context option e.g. dotnet ef dbcontext scaffold "Server=.\;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -c "AdventureContext"
<p><code style="display:block;white-space:pre-wrap">
//BasicCar.cs
using System;
namespace PrototypePattern
{
public abstract class BasicCar
{
public string ModelName{get;set;}
public int Price {get; set;}
public static int SetPrice()
{
int price = 0;
Random r = new Random();
int p = r.Next(200000, 500000);
price = p;
return price;
}
public abstract BasicCar Clone();
}
}
//Nano.cs
using System;
namespace PrototypePattern
{
public class Nano:BasicCar
{
public Nano(string m)
{
ModelName = m;
}
public override BasicCar Clone()
{
return (Nano) this.MemberwiseClone();//shallow Clone
}
}
}
//Ford.cs
using System;
namespace PrototypePattern
{
public class Ford:BasicCar
{
public Ford(string m)
{
ModelName = m;
}
public override BasicCar Clone()
{
return (Ford)this.MemberwiseClone();
}
}
}
//Client
using System;
namespace PrototypePattern
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***Prototype Pattern Demo***\n");
//Base or Original Copy
BasicCar nano_base = new Nano("Green Nano") {Price = 100000};
BasicCar ford_base = new Ford("Ford Yellow") {Price = 500000};
BasicCar bc1;
//Nano
bc1 = nano_base.Clone();
bc1.Price = nano_base.Price+BasicCar.SetPrice();
Console.WriteLine("Car is: {0}, and it's price is Rs. {1} ",bc1.ModelName,bc1.Price);
//Ford
bc1 = ford_base.Clone();
bc1.Price = ford_base.Price+BasicCar.SetPrice();
Console.WriteLine("Car is: {0}, and it's price is Rs. {1}", bc1.ModelName, bc1.Price);
Console.ReadLine();
}
}
}
</code></p>
var merged = new List<Person>(list1);
merged.AddRange(list2.Where(p2 =>
list1.All(p1 => p1.Number != p2.Number)));
It’s not all that obvious how it works and it also has performance problems. This solution has to iterate over list1 for every object in list2. Not every object every time but on average half of them (if the lists contains no duplicates within themselves). If the lists contains a 1000 objects each there’s a good chance that list1 will be iterated 500.000 times. That kind of work does not come for free.
We need to get rid of this nested looping. I was pretty sure a dictionary based solution would do the trick.
var dict = list2.ToDictionary(p => p.Number);
foreach (var person in list1)
{
dict[person.Number] = person;
}
var merged = dict.Values.ToList();
This solution converts list2 to a dictionary and then it loops a single time over list1. It either adds to the dictionary or replaces the value of list2 if it was a duplicate. This seemed to be a much more efficient algorithm.
In C# there’s not just Dictionary<TKey, TValue> but we also have HashSet. A HashSet is a collection that contains no duplicates. It also has the UnionWithmethod and that is exactly what we are looking for. The problem is that it compares object to object and we want to compare based on a property.
Fortunately the HashSet methods honors the default equality comparer. Such a beast could look like this.
var hs = new HashSet<Person>(list1, new PersonComparer());
hs.UnionWith(list2);
var merged = hs.ToList();
Set theory tells us that this merging is actually a union of the two sets (without duplicates) and LINQ happens to have a Union method.
var merged = list1.Union(list2, new PersonComparer());
By that we’ve come full circle and it’s time to see how the different solutions performs. I wrote a small test program that creates two lists of objects with 1000 objects in each. In each list 500 of the objects property, that we base the merge on, has the same value.
Lists and LINQ merge: 4820ms
Dictionary merge: 16ms
HashSet and IEqualityComparer: 20ms
LINQ Union and IEqualityComparer: 24ms
The first solution is, in this case, 300 times slower than the fastest one!
I prefer LINQ Union because it communicates intent very clearly.