【发布时间】:2012-11-16 12:11:29
【问题描述】:
- 火鸟 2.5
- 实体框架 5
- FirebirdClientDll 3.0.0.0
您好,我正在尝试使用实体框架(代码优先)访问我的旧数据库。 我遇到了数据库不使用外键的问题...
public class CUSTOMERS
{
public int CUSTOMERID { get; set; }
public string NAME{ get; set; }
}
public class INVOICES
{
public int INVOICEID{ get; set; }
public int CUSTOMERID{ get; set; }
public virtual CUSTOMERS CUSTOMERS { get; set; }
}
public class INVOICEContext : DbContext
{
public DbSet<CUSTOMERS> CUSTOMERS{ get; set; }
public DbSet<INVOICES> INVOICES{ get; set; }
public INVOICEContext(DbConnection connectionString) : base(connectionString, false)
{
Database.SetInitializer<INVOICEContext>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
/*modelBuilder.Entity<INVOICES>().HasRequired(b => b.CUSTOMERS)
.WithMany()
.Map(p => p.MapKey("INVOICEID"));*/ //Doesn't work because INVOICEID is defined
modelBuilder.Entity<INVOICES>().HasKey(a => new { a.INVOICEID, a.CUSTOMERID});
modelBuilder.Entity<CUSTOMERS>().HasKey(a => new { a.CUSTOMERID });
base.OnModelCreating(modelBuilder);
}
}
通常我可以从类INVOICES 中删除属性CUSTOMERID,但在这种情况下,它是主键的一部分...
我发现很多帖子建议使用IsIndependent,但似乎是removed from the Entity Framework 5 (or 4.1)。
希望你能看懂我蹩脚的英语,或许能给我提示一下我做错了什么^^
【问题讨论】:
标签: entity-framework ef-code-first entity-framework-5 firebird2.5