首先讓我對冗長的道歉後。我想提供更多細節將幫助你瞭解情況。映射EF代碼第一次到現有的數據庫
我試圖開發使用EF 5碼第一,一切映射到一個現有的數據庫,我的應用程序的數據層。我想要使用DbContext,POCO,流暢的表映射等等,但是我們現有的數據庫(表格)並不是直截了當的。所以,我遇到了一些麻煩。我現在試着簡要描述一下情況,然後再問幾個問題。
表
AlarmDefinition
AlarmDefinitionID int (primary key) (is identity)
...
Alarm
AlarmID int (primary key) (is identity)
AlarmDefinitionID int (sort of a foreign key) (is nullable)
SampleTagID int (is nullable)
SampleTime DateTime (is nullable)
...
ReasonAction
Time DateTime (primary key)
TagID int (primary key)
ReasonActionID int (primary key)
ReasonActionDefinition
ReasonActionID int (primary key) (is identity)
...
所以,一個AlarmDefinition可以有多個報警。而且,一個ReasonActionDefinition可以有許多ReasonActions。警報與ReasonAction之間有一種隱含的關係(即,一個警報可以有多個ReasonAction)。這是我遇到麻煩最多的地方。
波蘇斯
AlarmDefinition
public class AlarmDefinition
{
public int AlarmDefinitionID { get; set; }
...
public virtual ICollection<Alarm> Alarms { get; set; }
}
Alarm
public class Alarm
{
public int AlarmID { get; set; }
public Nullable<int> SampleTagID { get; set; }
public Nullable<System.DateTime> SampleTime { get; set; }
...
public Nullable<int> AlarmDefinitionID { get; set; }
public virtual AlarmDefinition AlarmDefinition { get; set; }
// I don’t know if this is set up correctly
public virtual ICollection<ReasonAction> ReasonActions { get; set; }
}
ReasonAction
public class ReasonAction
{
public System.DateTime Time { get; set; }
public int TagID { get; set; }
public virtual Alarm Alarm { get; set; }
public int ReasonActionID { get; set; } // Primary key
public virtual ReasonActionDefinition ReasonActionDefinition { get; set; }
}
ReasonActionDefinition
public class ReasonActionDefinition
{
public int ReasonActionID { get; set; }
...
public virtual ICollection<ReasonAction> ReasonActions { get; set; }
}
的DbContext
public class AppDbContext : DbContext
{
public DbSet<Alarm> Alarms { get; set; }
public DbSet<AlarmDefinition> AlarmDefinitions { get; set; }
public DbSet<ReasonAction> ReasonActions { get; set; }
public DbSet<ReasonActionDefinition> ReasonActionDefinitions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AlarmMap());
modelBuilder.Configurations.Add(new AlarmDefinitionMap());
modelBuilder.Configurations.Add(new ReasonActionMap());
modelBuilder.Configurations.Add(new ReasonActionDefinitionMap());
}
}
流利的映射
AlarmDefinitionMap
public class AlarmDefinitionMap : EntityTypeConfiguration<AlarmDefinition>
{
public AlarmDefinitionMap()
{
this.HasKey(t => t.AlarmDefinitionID);
...
this.ToTable("AlarmDefinition");
this.Property(t => t.AlarmDefinitionID).HasColumnName("AlarmDefinitionID");
...
}
}
AlarmMap
public class AlarmMap : EntityTypeConfiguration<Alarm>
{
public AlarmMap()
{
this.HasKey(t => t.AlarmID);
...
this.ToTable("Alarm");
this.Property(t => t.AlarmID).HasColumnName("AlarmID");
this.Property(t => t.AlarmDefinitionID).HasColumnName("AlarmDefinitionID");
this.Property(t => t.SampleTagID).HasColumnName("SampleTagID");
this.Property(t => t.SampleTime).HasColumnName("SampleTime");
...
this.HasOptional(t => t.AlarmDefinition)
.WithMany(d => d.Alarms)
.HasForeignKey(t => t.AlarmDefinitionID);
}
}
ReasonActionMap
public class ReasonActionMap : EntityTypeConfiguration<ReasonAction>
{
public ReasonActionMap()
{
this.HasKey(t => new { t.Time, t.TagID, t.ReasonActionID });
this.Property(t => t.TagID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.ReasonActionID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.ToTable("ReasonAction");
this.Property(t => t.Time).HasColumnName("Time");
this.Property(t => t.TagID).HasColumnName("TagID");
this.Property(t => t.ReasonActionID).HasColumnName("ReasonActionID");
// Relationships
// Not sure if this is correct since the related column names in the Alarm table are SampleTagId, not TagID and SampleTime, not Time. And SampleTagID and SampleTime in the Alarm table are not a primary keys.
// There's an implicit one-to-many relationship between the Alarm and ReasonAction entities.
this.HasRequired(t => t.Alarm)
.WithMany(d => d.ReasonActions)
.HasForeignKey(t => new { t.TagID, t.Time });
this.HasRequired(t => t.ReasonActionDefinition)
.WithMany(d => d.ReasonActions)
.HasForeignKey(t => t.ReasonActionID);
}
}
ReasonActionDefinitionMap
public class ReasonActionDefinitionMap : EntityTypeConfiguration<ReasonActionDefinition>
{
public ReasonActionDefinitionMap()
{
this.HasKey(t => t.ReasonActionID);
...
this.ToTable("ReasonActionDefinition");
this.Property(t => t.ReasonActionID).HasColumnName("ReasonActionID");
...
}
}
呼!這是很多東西。無論如何,這裏有一些問題,我們的數據庫和製圖的事情EF:1.沒有聲明引用完整性 - 在觸發器或遺留應用程序代碼都進行處理,2告警表,SampleTagID和SampleTime不是主鍵,但這兩列是一側爲與ReasonAction表中隱含的一個一對多的關係,3,報警和ReasonAction表之間的列名不匹配(SampleTagID和SampleTime報警表和時間標籤識別爲ReasonAction表)。
所以,我的問題是:A。鑑於上述情況,我可以讓EF代碼先與我現有的數據庫和表?B.如何我需要改變我的代碼,以使一對一很多關係在Alarm和ReasonAction表之間起作用(所以當我查詢Alarms時,ReasonActions導航屬性填充了所有相關記錄)?C.其他任何建議?
謝謝!