Eric
Eric

Reputation: 2128

Database/Object Mapping

This is a beginner question, but it's been frustrating me... I am using C#, by the way.

I'd like to make a few classes, each with their own properties and methods. I would also like to have a database to store certain instances of these classes in case I would ever need to look at them again. So, for example...

class Polygon
{
    String name;
    Double perimiter;
    int numSides;
    public Double GetArea()
    {
        // ...
    }
}

class Circle
{
    String name;
    Double radius;
    public void PrintName()
    {
        // ...
    }
}

Say I've got these classes. I also want a database that has the TABLES "Polygon" and "Circle" with the COLUMNS "name" "perimeter" "radius" etc. And I want an easy way to save a class instance into the database, or pull a class instance out of the database.

I have previously been using MS Access for my database stuff, which I don't mind using, but I would prefer if nothing other than .NET need to be installed.

I've been researching online a bit, but I wanted to get some opinions on here. I have looked at Linq-to-Sql, but it seems you need Sql-Server. Is this true? If so, I'd really rather not use it because I don't want to have to have it installed everywhere.

Anway, I'm just fishing for some ideas/insights/suggestions/etc. so please help me out if you can.

Thanks.

Upvotes: 0

Views: 4173

Answers (6)

Steven K.
Steven K.

Reputation: 2106

I would recommend using SQL CE 4.0 (public beta 2) and EF code first (CTP 5)

EF: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=35adb688-f8a7-4d28-86b1-b6235385389d

SQL CE: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=d9d933e7-9376-445e-8217-0c1e102a380e

Don't let the beta state scare you, both products are already very stable and if it's for a hobby project, or just learning, I wouldn't hesitate.

Creating/quering a database can be as simple as

using System;
using System.Data.Entity;
using System.Data.Entity.Database;
using System.Linq;

namespace EFCodeFirst
{
    public class Polygon
    {
        public int ID { get; set; }
        public String Name { get; set; }
        public Double Perimiter { get; set; }
    }

    public class Circle
    {
        public int ID { get; set; }
        public String Name { get; set; }
        public Double Radius { get; set; }
    }

    public class ShapeDbContext : DbContext
    {
        public DbSet<Polygon> Polygons { get; set; }
        public DbSet<Circle> Circles { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DbDatabase.SetInitializer(new CreateDatabaseIfNotExists<BookDbContext>());

            using(var context = new ShapeDbContext())
            {
                // creating a new object
                context.Polygons.Add(new Polygon { Name = "P1", Perimiter = 3 });
                context.Polygons.Add(new Polygon { Name = "P2", Perimiter = 2 });

                context.SaveChanges();
            }

            using(var context = new ShapeDbContext())
            {
                // creating a new object
                var polygons = context.Polygons.Where(o => o.Perimiter < 3);

                Console.WriteLine(polygons.Count());
            }
        }
    }
}

And this is the connection string you need to add to app.config for it to work

<connectionStrings> <add name="ShapeDbContext" connectionString="Data Source=|DataDirectory|\Database.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

One major drawback is that you cannot update an existing database - you'll have to do it manually - however that should be fixed with the final release of EF Code First... somewhere in january hopefully... Some majar plusses: don't need much code for simple stuff, you can still digg deep, Linq is fun, EF Code First is fun... Give it a try!

Upvotes: 2

slash shogdhe
slash shogdhe

Reputation: 4187

no its not necessory that u shud have sql server for linq to sql,u can also use add>>newItem>>localdatabse in ur project.and u can use linq to sql with it.

Upvotes: 1

Donnie
Donnie

Reputation: 46913

Entity Framework with SQL Server Express would suit your needs. EF may be overkill, but SQLServer Express can easily be installed by your application installer on the local machine and provides a much richer DB feature set than access does.

You may also want to look at SQLLite, which is an embedded database which requires no installation, just the distribution of a few more libraries.

Upvotes: 2

John McDonald
John McDonald

Reputation: 1799

I've never used it myself, but I know that NHibernate does what you are asking: http://www.nhibernate.com/

Upvotes: 1

Sorax
Sorax

Reputation: 2203

LINQ2SQL is ideal for 1 to 1 object relational mappings. That is, where the properties of the object are persisted as columns in a database. Which appears to be your situation.

The native provider for LINQ2SQL is naturally for SQL Server but there are now 3rd party providers for Oracle, MySql and PostGreSQL.

Using LINQ2SQL with SQL Server means you just need an instance of the database in a location addressable by your program. SQL Server itself will only need to be installed at that location.

Upvotes: 1

scott
scott

Reputation: 3071

You can always serialize the objects to xml files, then deserialize them when you want to get them back.

I know you said you want a database, but I'm not sure how you can have a database without installing one.

Upvotes: 0

Related Questions