Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:15:41+00:00 2026-06-12T09:15:41+00:00

First let me apologize for the lengthy post. I figured giving more details would

  • 0

First let me apologize for the lengthy post. I figured giving more details would help you understand the situation.

I’m trying to develop the data layer of my application using EF 5 Code First and mapping everything to an existing database. I’m wanting to use DbContext, POCOs, fluent table mappings, etc., but our existing database (the tables) isn’t straight-forward. So, I’m having trouble with a few things. I’ll try to briefly describe the situation now, then ask a couple questions later.

Tables

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)
...

So, one AlarmDefinition can have many Alarms. And, one ReasonActionDefinition can have many ReasonActions. There is an implicit relationship between Alarm and ReasonAction (i.e., one Alarm can have many ReasonActions). This is where I’m having the most troubles.

POCOs

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());
    }
}

Fluent Mappings

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");
        ...
    }
}

Whew! That was a lot of stuff. Anyway, here are some issues with our database and mapping things to EF: 1. No declarative referential integrity – all handled in triggers or legacy application code, 2. In the Alarm table, SampleTagID and SampleTime are not primary keys, yet these two columns are the one-side for an implicit one-to-many relation with the ReasonAction table, 3. Column names between the Alarms and ReasonAction tables do not match (SampleTagID and SampleTime for the Alarm table and Time and TagID for the ReasonAction table).

So, my questions are: A. Given the situation described above, can I make EF code first work with my existing database and tables?, B. How do I need to change my code to make the one-to-many relationship work between the Alarm and ReasonAction tables (so that when I query for Alarms the ReasonActions navigation property is populated with all related records)?, C. Any other suggestions?

Thanks!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:15:42+00:00Added an answer on June 12, 2026 at 9:15 am

    We ended up ditching EF for now and writing our own custom ADO.NET code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First let me apologize a bit for the length of this post, it's mostly
First I would like to apologize, I'm giving so much information to make it
First let me apologize for the scale of this problem but I'm really trying
First, let me apologize if this is the world's stupidest question. But, I'm stumped
I apologize for a lengthy question, but I really need your help. As a
First off, let me apologize if this has been asked already, but I can’t
Just want to let everyone know this is the first app I am trying
First off let me apologize to the SO community for coming to you with
First off, let me apologize if I'm writing this in the wrong place. I
Let me first apologize if this question could sound perhaps sort of amateurish for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.