Here are the generated models:
public class DbObjectMap : EntityTypeConfiguration<DbObject>
{
public DbObjectMap()
{
// Primary Key
HasKey(t => new { t.Type, CompanyName = t.CompanyName, Id = t.Id });
// Properties
Property(t => t.Timestamp)
.IsRequired()
.IsFixedLength()
.HasMaxLength(8)
.IsRowVersion();
Property(t => t.Type)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(t => t.CompanyName)
.IsRequired()
.HasMaxLength(30);
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Name)
.IsRequired()
.HasMaxLength(30);
this.Property(t => t.VersionList)
.IsRequired()
.HasMaxLength(80);
this.Property(t => t.LockedBy)
.IsRequired()
.HasMaxLength(132);
// Table & Column Mappings
this.ToTable("Object");
this.Property(t => t.Timestamp).HasColumnName("timestamp");
this.Property(t => t.Type).HasColumnName("Type");
this.Property(t => t.CompanyName).HasColumnName("Company Name");
this.Property(t => t.Id).HasColumnName("ID");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.Modified).HasColumnName("Modified");
this.Property(t => t.Compiled).HasColumnName("Compiled");
this.Property(t => t.BlobReference).HasColumnName("BLOB Reference");
this.Property(t => t.BlobSize).HasColumnName("BLOB Size");
this.Property(t => t.DbmTableNo).HasColumnName("DBM Table No_");
this.Property(t => t.Date).HasColumnName("Date");
this.Property(t => t.Time).HasColumnName("Time");
this.Property(t => t.VersionList).HasColumnName("Version List");
this.Property(t => t.Locked).HasColumnName("Locked");
this.Property(t => t.LockedBy).HasColumnName("Locked By");
}
}
and:
{
public ObjectTrackingMap()
{
// Primary Key
HasKey(t => new { ObjectType = t.ObjectType, Id = t.Id, ChangeType = t.ChangeType });
// Properties
Property(t => t.Timestamp)
.IsRequired()
.IsFixedLength()
.HasMaxLength(8)
.IsRowVersion();
Property(t => t.ObjectType)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(t => t.ChangeType)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// Table & Column Mappings
ToTable("Object Tracking");
Property(t => t.Timestamp).HasColumnName("timestamp");
Property(t => t.ObjectType).HasColumnName("Object Type");
Property(t => t.Id).HasColumnName("Object ID");
Property(t => t.ChangeType).HasColumnName("Change Type");
Property(t => t.ObjectTimestamp).HasColumnName("Object Timestamp");
}
}
These two objects don’t have any relation in database tables. But they could be joined by
Object.Type = ObjectTracking.ObjectType and Object.ID = ObjectTracking.ObjectID fields.
Is there any way to create correct mapping in EF using FluentAPI for these two entities?
Easiest way is with Visual Studio – just open your EDMX file in graphic designer mode (double click it), activate Toolbox and Properties windows (in View menu on top), selct “Association” from Toolbox and drag-draw your relation from one table to another, than tweak it in the properties window.