13

I've defined a set of classes in the style of Entity Framework Code-First conventions and annotated the class properties with System.ComponentModel.DataAnnotations attributes.

Now I want to generate a SQL Server Compact Edition (SCSE) (4.0) database from this code.

What code is needed to do this and what assemblies need to be imported?

So far I've imported the C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Desktop{System.Data.SqlServerCe.dll,System.Data.SqlServerCe.Entity\System.Data.SqlServerCe.Entity.dll} dll files.

Any pointers appreciated. Articles and whatnot.

4 Answers 4

15

I found the solution. The critical thing is this line of code:

DbDatabase.DefaultConnectionFactory = new 
                      SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

Here's a full sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Proto
{
    using System.Data;
    using System.Data.Entity;
    using System.Data.Entity.Database;
    using System.Data.SqlServerCe;
    using System.ComponentModel.DataAnnotations;

    class Program
    {
        static void Main(string[] args)
        {
            DbDatabase.DefaultConnectionFactory = new 
                        SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
            DbDatabase.SetInitializer(
                         new DropCreateDatabaseIfModelChanges<ProtoCatalog>()
                  );

            using (var db = new ProtoCatalog())
            {
                var user = new User
                {
                    Name = "bob",
                    Password = "123",
                    Creation = DateTime.Now,
                    PrimaryEmailAddress = "[email protected]"
                };

                db.Users.Add(user);

                db.SaveChanges();

                Console.ReadKey();
            }
       }
    }

    public class ProtoCatalog : DbContext
    {
        public DbSet<User> Users { get; set; }
    }

    public class User
    {
        [Key, StringLength(50)]
        public string Name { get; set; }

        [Required, StringLength(100)]
        public string Password { get; set; }

        [Required, StringLength(320)]
        public string PrimaryEmailAddress { get; set; }

        [StringLength(320)]
        public string SecondaryEmailAddress { get; set; }

        [Required]
        public DateTime Creation { get; set; }

        public bool Active { get; set; }
    }
}

The API will probably change between EF Code-First CTP 5 and RTM though, but this is how it's done now.

I've made a small blog post about it.

Sign up to request clarification or add additional context in comments.

Comments

2

See this blog and links in the same (for example the ScottGu article): http://erikej.blogspot.com/2011/01/sql-server-compact-40-released.html

1 Comment

Thanks. I'll check those out.
2

The best way I found was to install NuGet and play with the EF CodeFirst and SQL CE 4 packages, there is also a sample you can install from NuGet that has code examples for all the db creation strategies. You would do something like:

install-package EFCodeFirst.SqlServerCompact
install-package EFCodeFirst.Sample

In the root of your project directory should be a file called "AppStart_SQLCEEntityFramework.cs" refer to that for examples.

1 Comment

Thanks! I just have Visual C# Express installed at home but that may change.
2

Another way of providing the DefaultConnectionProvider is by adding it in app.config.

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="System.Data.SqlServerCe.4.0" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.