Archive

Archive for March, 2011

Entity framework–code first

It is darn simple and straight forward to get started using entity framework code first. Lessons learned from ef code first tutorial.

  1. Create a new C# project in visual studio 2010
  2. If you do not have nuget, use tools –> extension manager to install it
  3. Use nuget to setup your project for entity framework; install-package entityframework
  4. Add a connection string in app.config
  5. Code something like below

class Person

{

    public int PersonId { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }

}

 

class MyContext : DbContext

{

    public DbSet<Person> Persons { get; set; }

}

 

class Program

{

    static void Main(string[] args)

    {

        Database.SetInitializer<MyContext>(

            new DropCreateDatabaseIfModelChanges<MyContext>());

 

        using (var ctx = new MyContext())

        {

            ctx.Persons.Add(new Person { Name = "Olle" + Guid.NewGuid(), Age = 23 });

            ctx.Persons.Add(new Person { Name = "Pelle" + Guid.NewGuid(), Age = 54 });

            ctx.Persons.Add(new Person { Name = "Sven" + Guid.NewGuid(), Age = 36 });

            int x = ctx.SaveChanges();

            Console.WriteLine("{0}", x);

 

            var persons = from p in ctx.Persons where p.Age > 25 select p;

            foreach (var p in persons)

            {

                Console.WriteLine(p.PersonId + " " + p.Name);

            }

        }

    }

}

Categories: Uncategorized

Powershell – feel the power

I find it amusing and inspiring to follow the codeplex daily summary. Today I stumbled upon a project called file endings. A C# application claiming the following features:

  • Searching for all file-endings in a folder with its subfolders
  • showing all files with a choosen file-ending
  • Options:
    • Search-Option: Casesensitive (On/Off). If casesensitive is on .MP3 is not the same as .mp3. If its off it’s the same.
    • Delete all files with the choosen file-ending.

To clarify, file endings in this context means file extensions. To me, the features provided are just simple one liners in powershell.

# Search for all extensions in a folder and its sub folders
dir -r | group extension

# Show all files with a choosen extension (in this example .cs)
1: dir –r –filter *.cs  
2: dir –r | ? { $_.Extension –eq “.cs” }

(pipe result to out-gridview for a fancy display)

# Case sensitive search
dir -r | ? { $_.Extension -cmatch "\.FIRST" }

# Delete matching files
dir –r –filter *.cs | del

Categories: Uncategorized

A memory game

On my daughters initiative we made a simple memory game today (meaning I wrote some code and she took pictures using her digital camera).

screenshot

Click on the image to download an installer. The source code is not really interesting but here it is anyway.

Categories: Uncategorized

Scripting Carmenta Engine 5 – Part 3

Carmenta Engine 5 configuration files (.px) are normally produced using Carmenta Studio. The interested user might have peeked into the file and found out that the objects are serialized to an xml dialect heavily influenced by XAML. This, of course, can be utilized in scripts. For example, assume we want to define an UTM10 coordinate reference system suitable for the lake tahoe sample data.

$snippet = [Carmenta.Engine.Configuration]::FromString(@”
    <Configuration Version=”5.1.0.0″ Namespace=”snippet” xmlns=”
http://www.carmenta.com/Engine5″>
      <Crs Name=”Nad27LakeTahoeUtm10N” Public=”true” Projection=”{ResourceReference Predefined=true,Value=Projections.Utm10N}”>
        <Crs.GeodeticDatum>
          <GeodeticDatum Ellipsoid=”{ResourceReference Predefined=true,Value=Ellipsoids.Clarke1866}”>
            <GeodeticDatum.DatumShift>
              <HelmertDatumShift DX=”-15.853″ DY=”148.791″ DZ=”188.423″/>
            </GeodeticDatum.DatumShift>
          </GeodeticDatum>
        </Crs.GeodeticDatum>
      </Crs>
    </Configuration> 
“@)

$snippet.GetPublicObject(“Nad27LakeTahoeUtm10N”)

Categories: Uncategorized

Scripting Carmenta Engine 5 – Part 2

Projecting points between different coordinate systems is a common GIS operation. Here are script snippets for conversion between WGS84 and RT90 using Carmenta Engine.

Python:

from Carmenta.Engine.Core import *

Runtime.initialize()
rtxy = Crs.fromAuthorityAndCode(“epsg”, “3847”)
wgs84 = Crs.wgs84LongLat
p = wgs84.projectTo(rtxy, Point(12,57))
print(p.x, p.y)
Runtime.shutdown()

Powershell:

[System.Reflection.Assembly]::LoadFrom(“C:\Program Files\Carmenta\Carmenta Engine 5 SDK\bin\cecore.dotnet.dll”)
[Carmenta.Engine.Runtime]::Initialize()

$rt90 = [Carmenta.Engine.Crs]::FromAuthorityAndCode(“epsg”, “3847”)
$wgs84 = [Carmenta.Engine.Crs]::Wgs84LongLat

$lolaPoint = new-object Carmenta.Engine.Point -ArgumentList 12, 57
$rt90Point = $wgs84.ProjectTo($rt90, $lolaPoint)

[Carmenta.Engine.Runtime]::Shutdown()

Categories: Uncategorized