I've been messing around with
Entity Framework Code First for .NET and it's really useful for making little databases, such as you would use in a InSim program. I figured I'd do a quick tutorial on using it, as it's really very simple.
First of all boot up Visual Studio 2010 and create a new C# Console Application project, call it what you like. Before we can use the EntityFramework we'll need to include the assemblies in our project, but this is easy using the .NET package manager, called
NuGet. In Visual Studio go to View > Other Windows > Package Manager Console and in the console window type the following command then hit enter.
Install-Package EntityFramework
This should download and install the EntityFramework for you automatically. Now we need to create a simple model to store in the database, which for the purposes of this example will represent a simple cruise server. Here is the code for the model
<?php
public class User {
public int ID { get; set; }
public string UserName { get; set; }
public string PlayerName { get; set; }
public int Cash { get; set; }
public virtual List<Car> Cars { get; set; }
}
public class Car {
public int ID { get; set; }
public int UserID { get; set; }
public string CarName { get; set; }
public User User { get; set; }
}
?>
We create a User object and a Car object, that will have a one-to-many relationship. One user will have many cars, one car will have one user. As the EntityFramework follows a convention over configuration coding style, the relationships between the keys will be setup for us automatically. We will be able to access a user's cars through the User.Cars property, and access a car's user through its Car.User property.
Next we need to create a DbContext for our model, which will represent our connection with the database, and also tell the EntityFramework which model classes to use. This is extremely simple code.
<?php
public class CruiseContext : DbContext {
public DbSet<User> Users { get; set; }
public DbSet<Car> Cars { get; set; }
}
?>
Finally we need to tell the EntityFramework which database we want to use to store our data. By default it uses a SQLServer Express instance to store it, but I'd rather use SQLServer Compact instead, as it's simpler. SQLServer Compact is a simple file-based database, similar in nature to SQLite, that requires no installation or configuration. It can store up to 4GB of data if I remember correctly, so it's very useful for this kind of thing. Anyway, open you projects app.config file and change to contents to look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<connectionStrings>
<add name="CruiseContext"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=CruiseDB.sdf"/>
</connectionStrings>
</configuration>
The important bit is the connectionStrings element, which we use to define which database our 'CruiseContext' is to use. The 'System.Data.SqlServerCe.4.0' bit tells it to use a SQLServer Compact 4.0 database, and 'CruiseDB.sdf' is used to specify the actual name of the database file where we want to store our data. If this file does not exist then the EntityFramework will create it for us. If we delete it, it will recreate it.
Now all that's left is to write some code that actually access our database and saves and retrieves some data in order to test (prove) that it's working.
<?php
class Program {
static void Main() {
// Save some data to the database.
SaveUserToDatabase();
// Read the data back out of the database.
RetrieveUserFromDatabase();
Console.ReadLine();
}
private static void SaveUserToDatabase() {
// Create database connection.
CruiseContext db = new CruiseContext();
// Create a new user.
User user = new User {
UserName = "DarkTimes",
PlayerName = "Alex",
Cash = 1000,
Cars = new List<Car>(), // empty car list
};
// Add some cars.
user.Cars.Add(new Car { CarName = "UF1" });
user.Cars.Add(new Car { CarName = "XFG" });
user.Cars.Add(new Car { CarName = "XRG" });
// Add the user to the database.
db.Users.Add(user);
// Save the changes to the database.
db.SaveChanges();
}
private static void RetrieveUserFromDatabase() {
// Create new database connection.
CruiseContext db = new CruiseContext();
// Query the database for the user.
User user = (from u in db.Users
where u.UserName == "DarkTimes"
select u).Single();
// Write the user info to the console.
Console.WriteLine("UserName: {0}", user.UserName);
Console.WriteLine("PlayerName: {0}", user.PlayerName);
Console.WriteLine("Cash: {0}", user.Cash);
Console.WriteLine("Cars:");
foreach (Car car in user.Cars) {
Console.WriteLine(car.CarName);
}
}
}
?>
It might take a few moments for the program to run the first time you start it, as it needs to create the database and all the tables etc.. but after that the performance should be fine.
Well, it looks like more work than it really is the first time you see it, once you've tried it, however, you'll see that it makes it very simple and easy to create a database for your InSim app. I've found it very useful.
Here is the complete project to download.