Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体类型';报警接收器';需要定义主键_C#_Entity Framework Core - Fatal编程技术网

C# 实体类型';报警接收器';需要定义主键

C# 实体类型';报警接收器';需要定义主键,c#,entity-framework-core,C#,Entity Framework Core,我有两个模型,都有多对一关系和多对多关系。 用户可以创建多个报警,但一个报警只能有一个创建者。 一个用户可以接收多个警报,警报有多个接收器 public class AppUser { public Guid Id { get; set; } public virtual IEnumerable<Alarm> CreatedAlarms { get; set; } [InverseProperty("AlarmRecievers")]

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

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>();
    }
}
公共类AppUser
{
公共Guid Id{get;set;}
公共虚拟IEnumerable CreatedAlarms{get;set;}
[反向属性(“报警接收器”)]
公共虚拟IEnumerable ReceiveDalarms{get;set;}
公共应用程序用户()
{
CreatedAlarms=new HashSet();
ReceiveDalarms=新哈希集();
}
}

公共类报警
{
公共Guid Id{get;set;}
[外键(“AppUser”)]
公共Guid?AppUserId{get;set;}
公共AppUser AppUser{get;set;}
[反向财产(“接收武器”)]
公共虚拟IEnumerable AlarmReceivers{get;set;}
公共警报()
{
AlarmReceievers=新哈希集();
}  
}
但是当我尝试添加迁移时,我得到的标题是一个错误。我确实希望创建一个AlarmReceiver表,但考虑到我的代码中没有模型,我不知道如何创建键

早些时候我

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

但是我试图在我的代码中去掉连接表,所以我想按照我尝试的方式来做

,因为您遇到了这个错误,这意味着您使用的是EF Core,默认情况下不支持多对多关系,所以连接表是必须的。好的,谢谢。我错过了
modelBuilder.Entity<AlarmReciever>()
           .HasKey(c => new { c.AppUserId, c.AlarmId });