Yes, it is possible - if you do not rely on the creation or migration features (EF 5.0).
So, first configure EF to use the SQL CE provider using the app.config and a named configuration
<connectionStrings>
<add name="TestDatabase"
providerName="System.Data.SqlServerCe.3.5"
connectionString="Data Source=test.sdf"/>
</connectionStrings>
and
public class TestDbDataContext : DbContext
{
public TestDbDataContext() : base("TestDatabase") { }
}
disable the creation and migration functionality before you use your data context
Database.SetInitializer<TestDbDataContext>(null);
Now you can access the database. Obviously you will have to create the tables manually.
Caveat:
Inserting server generated keys does not work with CE 3.5. So you will have to manage them on the client side (best use GUIDs to prevent key conflicts). Furthermore, even if you plan to manage the keys yourself you need to watch out. Defining the key like this
[Key]
Guid Id { get; set; }
will lead EF to believe that the key is generated on the server side. Therefore, you need to define the key properties using EF's fluent API to set the DatabaseGeneratedOption
to None:
this.Property(p => p.ProductId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
.IsRequired();
this.HasKey(p => p.ProductId);
This can be either done in a configuration class (as in the sample) or in the data contex's OnModelCreating
function.