我有两个班级 Class1 和 Class2
[Table("CLASS1", Schema = "public")]
public class Class1
{
public int Class1Id { get; set; }
public string Name1 { get; set; }
}
[Table("CLASS2", Schema = "public")]
public class Class2
{
public int Class2Id { get; set; }
public string Name2 { get; set; }
public int Class1Id { get; set; }
public virtual Class1 c1 { get; set; }
我定义了 DatabaseContext :
public sealed class DatabaseContext : DbContext
{
private static readonly DatabaseContext instance = new DatabaseContext();
public static DatabaseContext Instance
{
get
{
return instance;
}
}
private DatabaseContext()
: base("maconnexion")
{
Database.SetInitializer<DatabaseContext>(null);
}
public DbSet<Class1> table1{ get; set; }
public DbSet<Class2> table2{ get; set; }
}
我已将这些行添加到我的 App.config
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
<connectionStrings>
<add name="maconnexion"
connectionString="Server=127.0.0.1;Port=5432;Database=DB_Ticketing;UserId=postgres;Password=slots2013;" providerName="Npgsql" />
</connectionStrings>
我在 PostgreSQL 中创建了一个名为 DB_Ticketing 的数据库。但是当我执行我的代码时,表 table1 和 table2 并没有添加到我的数据库中。