Ratio of EF to Slow Varying Measurements

I am using C # with .net 4.5.1 and EF 6 first.

Is there a way to mark the relationship with the time range of reality? In terms of SQL DDL, I would define something like this:

create table a_b_relationship (
  table_a_id int,
  table_b_id int,
  valid_from datetime,
  valid_to datetime
)

      

I am wondering if I have to define the relationship as a class myself so that EF translates it into a relationship table or contains a data annotation or a cursory api.

Thank you in advance

+3


source to share


1 answer


If I understand your question correctly, you want to add properties to a table created to support a many-to-many relationship between tables.

I would create a separate class to support this.

public class a_b_relationship 
{
  public a_b_relationship_id(table_a a, table_b b)
  {
    table_a = a;
    table_b = b;
    ValidTime(20);
  }

  public int a_b_relationship_id { get; set; }
  public int table_a_id { get; set; }
  public int table_b_id { get; set; }
  public DateTime valid_from { get; set; }
  public DateTime valid_to { get; set; }

  public virtual IEnumerable<table_a> table_as { get; set; }
  public virtual IEnumerable<table_b> table_bs { get; set; }


  private void ValidTime(int validMinutes)
  {
    valid_from = DateTime.Now();
    valid_to = DateTime.Now().AddMinutes(validMinutes);
  }
}

      

Then match this in your context.



This way you can add additional properties for the DateTimes expiration instead of letting EF generate the connection table without them.

If you are trying to set the time interval of a relationship in EF (for example, when I bind a record in table_a to a record in table_b, then this relationship is valid for 2 hours), then no. This is a business rule that you set in code.

Edit

I added a method for adding a time range that was available when the relationship was created. You still need to save changes and all, but this is just an example of how I would do it. You can configure this to work from one or both of classes a and b.

0


source







All Articles