实体类型“ AlarmReciever”要求定义主键

时间:2020-10-05 10:19:09

标签: c# entity-framework-core

我有两个模型,它们都具有多对一关系和多对多关系。 一个用户可以创建许多警报,但一个警报只能有一个创建者。 用户可以接收许多警报,并且警报有许多接收者。

public class AppUser
{
    public Guid Id { get; set; }
    public virtual IEnumerable<Alarm> CreatedAlarms { get; set; }
    [InverseProperty("AlarmRecievers")]
    public virtual IEnumerable<Alarm> RecievedAlarms { get; set; }

    public AppUser()
    {
        CreatedAlarms = new HashSet<Alarm>();
        RecievedAlarms = new HashSet<Alarm>();
    }
}

public class Alarm
{
    public Guid Id { get; set; }
    [ForeignKey("AppUser")]
    public Guid? AppUserId { get; set; }
    public AppUser AppUser { get; set; }
   
    [InverseProperty("RecievedAlarms")]
    public virtual IEnumerable<AppUser> AlarmRecievers { get; set; }
    public Alarm()
    {
        AlarmRecievers = new HashSet<AppUser>();
    }  
}

但是当我尝试添加迁移时,出现标题错误。我确实希望有一个创建的AlarmReciever表,但是考虑到我的代码中没有模型,所以我不知道如何创建密钥。

我以前有过

modelBuilder.Entity<AlarmReciever>()
           .HasKey(c => new { c.AppUserId, c.AlarmId });

但是我试图摆脱代码中的联接表,所以我想以尝试的方式做到这一点

0 个答案:

没有答案