That's right, I just a third minor update to Image Resizer 3 Preview 3. This one fixes a bug in the context menu handler that was causing problems with some other extensions. The update also includes some community translations to French and German.
Behind the scenes, this version also puts me in a good position to start tackling the remaining features that I have scheduled for version 3.
Brice's Blog
Highlighting some of my more technical adventures
Saturday, February 23, 2013
Wednesday, October 31, 2012
Entity Framework on PostgreSQL
This walkthrough will get you started with an application that uses the Entity Framework (EF) to read and write data from a PostgreSQL database. It is intended to be similar to the Code First to a New Database walkthrough.
There are currently two PostgreSQL providers for EF that I know of: Npgsql and Devart's dotConnect for PostgreSQL. Devart's provider has a much richer set of features, but it is also a commercial product. In the spirit of FOSS, we will be using the Npgsql provider for this walkthrough. I encourage you to keep the Devart provider in mind, however, if your project requires that extra level of support.
Add the following two classes to your project.
When I started writing this, Chinook Database actually wasn't available for PostgreSQL. Fortunately, it's an open source project so I've submitted a pull request that adds support for PostgreSQL. It was released as part of version 1.4.
There are currently two PostgreSQL providers for EF that I know of: Npgsql and Devart's dotConnect for PostgreSQL. Devart's provider has a much richer set of features, but it is also a commercial product. In the spirit of FOSS, we will be using the Npgsql provider for this walkthrough. I encourage you to keep the Devart provider in mind, however, if your project requires that extra level of support.
Create the Application
For simplicity, we will be using a Console Application, but the basic steps are the same regardless of project type.- Open Visual Studio
- Select File -> New -> Project...
- Select Console Application
- Name the project
- Click OK
Create the Model
For our model, we'll be borrowing pieces from the Chinook Database (a cross-platform, sample database). Specifically, we will be using Artists and Albums.Add the following two classes to your project.
public class Artist
{
public Artist()
{
Albums = new List<Album>();
}
public int ArtistId { get; set; }
public string Name { get; set; }
public virtual ICollection<Album> Albums { get; set; }
}
public class Album
{
public int AlbumId { get; set; }
public string Title { get; set; }
public int ArtistId { get; set; }
public virtual Artist Artist { get; set; }
}
Create a Context
In EF, the context becomes your main entry point into the database. Before we define our context though, we will need to install Entity Framework.- Select Tools -> Library Package Manager -> Package Manager Console
- Inside the Package Manager Console (PMC) run Install-Package EntityFramework
class ChinookContext : DbContext
{
public DbSet<Artist> Artists { get; set; }
public DbSet<Album> Albums { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Map to the correct Chinook Database tables
modelBuilder.Entity<Artist>().ToTable("Artist", "public");
modelBuilder.Entity<Album>().ToTable("Album", "public");
// Chinook Database for PostgreSQL doesn't auto-increment Ids
modelBuilder.Conventions
.Remove<StoreGeneratedIdentityKeyConvention>();
}
}
Install the Provider
In order to connect to PostgreSQL databases, we will need to install an appropriate ADO.NET and Entity Framework provider. Luckily, the provider we're using is available via NuGet.- Inside PMC, run Install-Package Npgsql
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider"
invariant="Npgsql"
description="Data Provider for PostgreSQL"
type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
Add the Database
Unfortunately, Npgsql does not support creating databases. So instead of letting Code First create our database, we will need to manually add a database to our project. It's a good thing we're using a cross-platform sample database!When I started writing this, Chinook Database actually wasn't available for PostgreSQL. Fortunately, it's an open source project so I've submitted a pull request that adds support for PostgreSQL. It was released as part of version 1.4.
- Download and extract the PostgreSQL version of the Chinook Database
- Run CreatePostgreSql.bat
<connectionStrings>
<add name="ChinookContext"
connectionString=
"Server=localhost;Database=chinook;User Id=postgres;Password=P4ssw0rd;"
providerName="Npgsql" />
</connectionStrings>
Start Coding
Ok, we should be ready to start coding our application. Let's see what artists exist in the database. Inside Program.cs, add the following to Main.using (var context = new ChinookContext())
{
var artists = from a in context.Artists
where a.Name.StartsWith("A")
orderby a.Name
select a;
foreach (var artist in artists)
{
Console.WriteLine(artist.Name);
}
}
Hmm, it looks like one of my favorite bands is missing. Let's add it.using (var context = new ChinookContext())
{
context.Artists.Add(
new Artist
{
ArtistId = 276,
Name = "Anberlin",
Albums =
{
new Album
{
AlbumId = 348,
Title = "Cities"
},
new Album
{
AlbumId = 349,
Title = "New Surrender"
}
}
});
context.SaveChanges();
}
We can also update and delete existing data like this.using (var context = new ChinookContext())
{
var police = context.Artists.Single(a => a.Name == "The Police");
police.Name = "Police, The";
var avril = context.Artists.Single(a => a.Name == "Avril Lavigne");
context.Artists.Remove(avril);
context.SaveChanges();
}
Conclusion
Hopefully by now, you have enough information to get started using the Entity Framework with a PostgreSQL database. For many, many more articles on how to use EF, check out our team's official Getting Started page on MSDN.
Labels:
Entity Framework,
PostgerSQL
Tuesday, October 16, 2012
Entity Framework on MySQL
This walkthrough will get you started with an application that uses the Entity Framework (EF) to read and write data from a MySQL database. It is intended to be similar to the Code First to a New Database walkthrough.
There are currently two MySQL providers for EF that I know of: Connector/Net and Devart's dotConnect for MySQL. Devart's provider has a few more features, but it is also a commercial product. In the spirit of FOSS, we will be using the Connector/Net provider for this walkthrough. I encourage you to keep the Devart provider in mind, however, if your project requires that extra level of support.
Add the following two classes to your project.
There are currently two MySQL providers for EF that I know of: Connector/Net and Devart's dotConnect for MySQL. Devart's provider has a few more features, but it is also a commercial product. In the spirit of FOSS, we will be using the Connector/Net provider for this walkthrough. I encourage you to keep the Devart provider in mind, however, if your project requires that extra level of support.
Create the Application
For simplicity, we will be using a Console Application, but the basic steps are the same regardless of project type.- Open Visual Studio
- Select File -> New -> Project...
- Select Console Application
- Name the project
- Click OK
Create the Model
For our model, we'll be borrowing pieces from the Chinook Database (a cross-platform, sample database). Specifically, we will be using Artists and Albums.Add the following two classes to your project.
public class Artist
{
public Artist()
{
Albums = new List<Album>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Album> Albums { get; set; }
}
public class Album
{
public int Id { get; set; }
public string Title { get; set; }
public int ArtistId { get; set; }
public virtual Artist Artist { get; set; }
}
Create a Context
In EF, the context becomes your main entry point into the database. Before we define our context though, we will need to install Entity Framework.- Select Tools -> Library Package Manager -> Package Manager Console
- Inside the Package Manager Console (PMC) run Install-Package EntityFramework -Version 4.2
Note: We're using EF 4.2 because there's a bug in the current version of Connecter/Net that prevents it from properly creating the database. Please see my workaround if you're interested in using the latest version of EF.Now, add the context class to your project.
class ChinookContext : DbContext
{
public DbSet<Artist> Artists { get; set; }
public DbSet<Album> Albums { get; set; }
}
Install the Provider
In order to connect to MySQL databases, we will need to install an appropriate ADO.NET and Entity Framework provider. Luckily, the provider we're using is available via NuGet.- Inside PMC, run Install-Package MySQL.Data.Entities
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description="Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
</DbProviderFactories>
</system.data>
Add a Connection String
In order to connect to the right database, we need to add a connection string to the App.Config. Anywhere inside the configuration element, add the following fragment.<connectionStrings>
<add name="ChinookContext"
connectionString=
"server=localhost;database=Chinook;User Id=root;password=P4ssw0rd"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Start Coding
Ok, we should be ready to start coding our application. Let's create some artists and albums.using (var context = new ChinookContext())
{
context.Artists.Add(
new Artist
{
Name = "Anberlin",
Albums =
{
new Album { Title = "Cities" },
new Album { Title = "New Surrender" }
}
});
context.Artists.Add(
new Artist
{
Name = "The Police",
Albums =
{
new Album { Title = "The Police Greatest Hits" }
}
});
context.Artists.Add(new Artist { Name = "Avril Lavigne" });
context.SaveChanges();
}
Now let's see how to read the data from the database.using (var context = new ChinookContext())
{
var artists = from a in context.Artists
where a.Name.StartsWith("A")
orderby a.Name
select a;
foreach (var artist in artists)
{
Console.WriteLine(artist.Name);
}
}
Finally, here is some code that updates and deletes some data.using (var context = new ChinookContext())
{
var police = context.Artists.Single(a => a.Name == "The Police");
police.Name = "Police, The";
var avril = context.Artists.Single(a => a.Name == "Avril Lavigne");
context.Artists.Remove(avril);
context.SaveChanges();
}
Conclusion
Hopefully by now, you have enough information to get started using the Entity Framework with a MySQL database. For many, many more articles on how to use EF, check out our team's official Getting Started page on MSDN.
Labels:
Entity Framework,
MySQL
Subscribe to:
Posts (Atom)