- 火鸟 2.5
- 实体框架 5
- 火鸟客户端DLL 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);
}
}
通常我可以CUSTOMERID
从类中删除该属性INVOICES
,但在这种情况下它是主键的一部分......
我发现了许多建议使用的线程IsIndependent
,但它似乎已从 Entity Framework 5 (或 4.1)中删除。
我希望你能理解我糟糕的英语,也许给我一个提示我做错了什么^^