Entity framework core EF Core 3.1-如何将已组合的PK用于另一个组合PK?

Entity framework core EF Core 3.1-如何将已组合的PK用于另一个组合PK?,entity-framework-core,Entity Framework Core,我目前正在学习使用EF,我有以下关系: 警报有1到n次发生。 每个事件可以有0到n个值(附加信息) 公共类警报 { //主键 public int AlertId{get;set;} //属性 public int CurrentAlertLevel{get;set;} public DateTime TimeRaised{get;set;} 公共日期时间时间限定{get;set;} //还有一些其他的属性。。。 //关系 公共ICollection引用{get;set;} } 公共类警报事件

我目前正在学习使用EF,我有以下关系:

警报有1到n次发生。 每个事件可以有0到n个值(附加信息)

公共类警报
{
//主键
public int AlertId{get;set;}
//属性
public int CurrentAlertLevel{get;set;}
public DateTime TimeRaised{get;set;}
公共日期时间时间限定{get;set;}
//还有一些其他的属性。。。
//关系
公共ICollection引用{get;set;}
}
公共类警报事件
{
//作为主键一部分的关系
public int AlertId{get;set;}
//属性
公共整数序号{get;set;}
//一些常用属性
//关系
公共ICollection AlertDetailValues{get;set;}
}
公共类AlertDetailValue
{
public int-AlertDetailValueId{get;set;}
公共整数顺序{get;set;}
公共字符串值{get;set;}
}
在ModelCreating上的DB上下文中,我正在为AlertOccurrence设置组合PK:

modelBuilder.Entity<AlertOccurrence>().HasKey(ao => new {ao.AlertId, ao.Ordinal});
modelBuilder.Entity().HasKey(ao=>new{ao.AlertId,ao.Ordinal});
虽然这似乎是可行的,但我实际上希望归档的是相同的关系,而不需要将AlertDetailValueId作为PK。EF生成的表还包括AlertOccurrenceAlertId和AlertOccurrenceOrdinal,这对我来说似乎是浪费空间

所以我想做的是: 具有AlertDetailValue的组合主键,该主键由AlertDetailValue.Order和AlertOccurrence的(已组合)主键组成,而不是“人工”AlertDetailValueId。这可能吗

我的部分问题可能是使用fluent api定义的PK不是数据类的一部分。所以可能要问的另一个问题是:有没有办法在实体类中使用fluent api中定义的键

或者我需要在实体类AlertDetailValue中包含AlertOccurrenceAlertId和AlertOccurrenceOrdinal,但我如何链接它们呢


正如我所说,我仍在努力了解EF,因此,尽管可能有更好的方法,但我对这种特殊的关系/组合(组合)PK感兴趣,即使它可能是学术性的。。。任何帮助都将不胜感激。

试图解释我试图做什么以及我的问题是什么——并好好洗个澡——帮助我向谷歌提出了不同的问题,并将更多注意力集中在外键上。 这并不是说我以前没有尝试用谷歌搜索它。。。我只是问错了问题

所以我发现: (在尝试新方法时@atiyar还暗示缺少外键…)

我的新解决方案是将AlertDetailValue更改为故意包含发生PK生成的部分:

    public class AlertDetailValue
    {
         // relations will be set up in fluent api in OnModelCreating of db context
        public int AlertOccurenceAlertId { get; set; }
        public int AlertOccurenceOrdinal { get; set; }

        public int Order { get; set; }
        public string Value { get; set; }
    }
然后告诉EF有一个组合PK和一个组合外键:

            modelBuilder.Entity<AlertDetailValue>().HasKey(adv => new { adv.AlertOccurenceAlertId, adv.AlertOccurenceOrdinal, adv.Order  });

            modelBuilder.Entity<AlertOccurrence>().HasMany<AlertDetailValue>(adv => adv.AlertDetailValues).WithOne()
                .HasForeignKey(adv => new {adv.AlertOccurenceAlertId, adv.AlertOccurenceOrdinal});
modelBuilder.Entity().HasKey(adv=>new{adv.alertoccurrencealertid,adv.alertoccurrenceordinal,adv.Order});
modelBuilder.Entity()有许多(adv=>adv.AlertDetailValues)
.HasForeignKey(adv=>new{adv.alertoccurrencealertid,adv.alertoccurrenceordinal});

如果EF生成的表包含诸如
AlertOccurrenceAlertId
AlertOccurrenceOrdinal
等列,那是因为您自己还没有定义/配置外键,EF正试图使用其命名约定为您这样做。
            modelBuilder.Entity<AlertDetailValue>().HasKey(adv => new { adv.AlertOccurenceAlertId, adv.AlertOccurenceOrdinal, adv.Order  });

            modelBuilder.Entity<AlertOccurrence>().HasMany<AlertDetailValue>(adv => adv.AlertDetailValues).WithOne()
                .HasForeignKey(adv => new {adv.AlertOccurenceAlertId, adv.AlertOccurenceOrdinal});