- Firebird 2.5
- Entity Framework 5
- FirebirdClientDll 3.0.0.0
I'm (still) trying to access my legacy database with the Entity Framework (Code First).
Now I want to create a one to may relationship without a Forrein Key.
public class KONTAKTE
{
public int KUNDENNR { get; set; }
public Int16 ANSPRNR { get; set; }
public Int16 NR { get; set; }
public virtual ICollection<KONTAKTBED> KONTAKTBED { get; set; }
}
public class KONTAKTBED
{
public int ID { get; set; }
public Int16 LFDNR { get; set; }
public int KUNDENNR { get; set; }
public Int16 ANSPRNR { get; set; }
public string NAME { get; set; }
}
public class CTKontakt : DbContext {
public DbSet<KONTAKTE> KONTAKTE { get; set; }
public DbSet<KONTAKTBED> KONTAKTBED { get; set; }
public CTKontakt(DbConnection connectionString) : base(connectionString, false)
{
Database.SetInitializer<CTKontakt>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
/* modelBuilder.Entity<KONTAKTE>().
HasMany(p => p.KONTAKTBED).
WithMany().
Map(t => t.MapLeftKey("KUNDENNR", "NR")
.MapRightKey("KUNDENNR", "LFDNR"));*/ //Does't work
modelBuilder.Entity<KONTAKTE>().HasKey(a => new { a.KUNDENNR, a.ANSPRNR, a.NR });
modelBuilder.Entity<KONTAKTBED>().HasKey(a => new { a.ID, a.DATABASE_ID});
base.OnModelCreating(modelBuilder);
}
As you can see I can't use the whole Primary Key of the KONTAKTE-Table. Does this mean I have to implemet a many to many realitonship? Currently I just join the tables later:
from k in lEKontakt.KONTAKTE
join kbed in lEKontakt.KONTAKTBED
on new { KUNDENNR = k.KUNDENNR, NR = k.NR }
equals new { KUNDENNR = kbed.KUNDENNR, NR = kbed.LFDNR }
I want to do something like this:
modelBuilder.Entity<KONTAKTE>()
.HasKey(d => new { d.KUNDENNR, d.ANSPRNR, d.NR })
.HasMany(d => d.KONTAKTBED)
.WithOptional()
.HasForeignKey(l => new { l.KUNDENNR, l.ANSPRNR, l.LFDNR });
But without the ANSPRNR...
I'm still new to Ef-Code First and all samples I find seem not to work under EF 5...