C# Asp.NET内核中的关系数据库SQL查询

C# Asp.NET内核中的关系数据库SQL查询,c#,entity-framework-core,C#,Entity Framework Core,我没有语法错误,但似乎无法通过这种方式从相关数据访问属性 基本上,目标是:用户创建一个列表,然后为该列表创建一些注释。然后他应该能够将其中一个注释分配给另一个用户。当其他用户登录时,他应该能够看到分配给他的新便笺 有人能帮我解决这个问题吗 public async Task<List<Note>>ShowAssigned() { return await _context.Notes .Where(x => x.List.OwnerId !=

我没有语法错误,但似乎无法通过这种方式从相关数据访问属性

基本上,目标是:用户创建一个列表,然后为该列表创建一些注释。然后他应该能够将其中一个注释分配给另一个用户。当其他用户登录时,他应该能够看到分配给他的新便笺

有人能帮我解决这个问题吗

public async Task<List<Note>>ShowAssigned()
{
 return await _context.Notes
           .Where(x => x.List.OwnerId != x.OwnerId)
           .ToListAsync()
}
和上下文类:

public class List
{
    public Guid ListId { get; set; }
    public string OwnerId { get; set; }
    public List<Note> Notes { get; set; }
}



public class Note
{
    public Guid ID { get; set; }
    public string OwnerId { get; set; }
    [ForeignKey("ListId")]
    public Guid ListId { get; set; }
    public List List { get; set; }
}
当我尝试在这样的视图中以相同的方式访问数据时:

public DbSet<Note> Notes { get; set; }
public DbSet<List> Lists { get; set; }
运行web应用程序时出现此错误无语法错误: NullReferenceException:对象引用未设置为对象的实例

如果您的项目位于ASP.NET Core>=2.1上 然后在Startup类中的ConfigureServices方法中:


因此,在TanvirArjel的帮助下,我找到了问题的答案,在某些方面,但我基本上做得不同

await _context.Notes.ToListAsync()

非常感谢你的努力。我更改了我的类,现在我得到了更新数据库:无法将参数绑定到参数“Path”,因为它为null。关于迁移,我想创建一个新的database@Nolan现在我的电脑坏了!半小时后我有空。然后给我团队观众Id和通行证,我会解决你的问题!我可以向你保证我的代码没有错。问题在于项目配置中可能存在。此时您可以尽最大努力//您不再需要OwnerId,因为它已经存在于List类中。你可以删除它,如果你想的话,应该可以为笔记指定一个新的所有者,而CoreSponding列表应该保留它的所有者,然后不要删除它!问题基本上是显示分配给另一个所有者但仍然是列表的一部分的注释,该列表的所有者由于分配了Being而不再具有访问权限。由于ListId不能作为外键为Null,所以我不能将其设置为Null。是否可以将其设置为特定的持久Guid,这对于分配给其他所有者的所有项目都是相同的?
public class List
{
    public Guid ListId { get; set; }
    public string OwnerId { get; set; }

    public virtual List<Note> Notes { get; set; }
}

public class Note
{
    public Guid ID { get; set; }
    public string OwnerId { get; set; }

    [ForeignKey("List")] // Not ListId, its List
    public Guid ListId { get; set; }

    public virtual List List { get; set; }
}
await _context.Notes.Include(n => n.List).ToListAsync()
services.AddDbContext<ApplicationDbContext>(options =>
   options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
await _context.Notes.ToListAsync()
 public async Task<List<Note>> GetAssignedItemsAsync(ApplicationUser user)
    {

        var lists = await _context.Lists.Include(l => l.Notes).Where(x => x.OwnerId != user.Id).ToListAsync();

        var notesListe = new List<Note>();

        foreach (List l in lists)
        {
            foreach (Note n in l.Notes)
            {
                if (n.OwnerId == user.Id)
                {
                    notesListe.Add(n);
                }
            }
        }
        return  notesListe;

    }