SQL Server Compact Code First: Database not created

code-first ef-code-first entity-framework-6 sql-server-ce

Question

I want to use Entity Framework code first to create a SQL Server Compact DB. I first created the model classes, then the DB context:

public class Peng_Rob_BinaryInteractions
{
    [Key]
    public int PID { get; set; }
    public int ID1 { get; set; }
    public int ID2 { get; set; }
    public Nullable<double> Value { get; set; }
}
public class RefData
{
    [Key]
    public int ID { get; set; }
    public Nullable<double> Mwt { get; set; }
    public Nullable<double> Tc { get; set; }
    public Nullable<double> Pc { get; set; }
    public Nullable<double> Omega { get; set; }
    public Nullable<double> Hoffset { get; set; }
    public Nullable<double> P1 { get; set; }
    public Nullable<double> P2 { get; set; }
    public Nullable<double> P3 { get; set; }
    public Nullable<double> P4 { get; set; }
    public Nullable<double> P5 { get; set; }
    public Nullable<double> P6 { get; set; }
    public Nullable<double> P7 { get; set; }
    public Nullable<double> TC_K { get; set; }
    public Nullable<double> PC_K { get; set; }
    public Nullable<double> Tr { get; set; }
    public Nullable<double> Ha_a { get; set; }
    public Nullable<double> Ha_b { get; set; }
    public Nullable<double> Ha_c { get; set; }

    public Nullable<double> Entropy_Offset { get; set; }
    public string Name { get; set; }
}

The DbContext class:

public class AddinDBContext : DbContext
{
    public DbSet<Peng_Rob_BinaryInteractions> Peng_Rob_BinaryInteractions { set; get; }
    public DbSet<RefData> RefData { set; get; }

    static AddinDBContext()
    {
        Database.SetInitializer<AddinDBContext>(new DbInitializer());
        //using (AddinDBContext db = new AddinDBContext())
        //    db.Database.Initialize(false);
    }

    public static AddinDBContext Create()
    {
        return new AddinDBContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

And:

public class DbInitializer : DropCreateDatabaseAlways<AddinDBContext>{
    //Seed method here
}

And inside the app.config file, I added the connection string:

<add name="AddinDBContext" 
        connectionString="Data Source=D:\GTDB.sdf"
        providerName="System.Data.SqlServerCe.4.0" />

I debugged the code and there is no exception, but I can't find the database inside the d at all or any other place; the database is not created. What am I doing wrong?

1
0
8/6/2018 3:21:32 PM

Accepted Answer

You need to add the connection string to the startup project.

0
8/7/2018 10:23:59 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow