Chase Florell
Chase Florell

Reputation: 47377

How can I name my Database using EF Code First?

I've got my EF Code First working exactly as expected aside from one small bit. I'm not sure how to name my Database File.

I'm using SQL CE, but I'm sure this applies to all forms of EF Code First.

Here's my DbContext

namespace MyApp.Domain.EntityFramework
{
    public class DataContext : DbContext
    {
        //...
    }
}

And when the database is created it's created as

MyApp.Domain.EntityFramework.DataContext.sdf

I'd prefer to just have it named

MyApp.sdf

Now I'm sure this is simple, but my Googling skills keep turning up examples where the database name is auto generated like mine.

http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx

Upvotes: 1

Views: 1585

Answers (2)

John
John

Reputation: 1296

I was looking to do the same. Managed to end up with this:

public class ShopDbContext : DbContext
    {
        public DbSet<Product> Products { get; set; }

        public DbSet<Feature> Features { get; set; }

        public DbSet<Category> Categories { get; set; }

        public DbSet<Subcategory> Subcategories { get; set; }

        public DbSet<Information> OrderInformation { get; set; }

        public ShopDbContext() : base("Shop")
        {
        }
    }

It will name your database "Shop" so just replace what is in the base("Shop") with whatever you want to call your database. Hope this helps.

Upvotes: 2

Diego Mijelshon
Diego Mijelshon

Reputation: 52725

You need to specify a connection string (for example by creating a connection string named DataContext (your class name) in your config file, and set the desired name there.

Upvotes: 2

Related Questions