SQL Server Compact代码优先:未创建数据库

时间:2018-08-06 14:06:56

标签: entity-framework-6 ef-code-first sql-server-ce code-first

我想首先使用实体​​框架代码来创建SQL Server Compact DB。 我首先创建了模型类,然后创建了数据库上下文:

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; }
}

DbContext类:

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);
    }
}

并且:

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

app.config 文件中,我添加了连接字符串:

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

我调试了代码,也没有例外,但是我根本找不到d内或任何其他地方的数据库;数据库未创建。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您需要将连接字符串添加到启动项目中。