查询用户是否订阅指定新闻的方法和途径return
Querying method and approach to check if user is subscribed to specified news and return them
我目前正在使用 Entity Framework Core 和 Net Core 2.1 网络应用程序学习如何指示用户是否订阅了指定新闻。
我花了一天的时间试图了解如何在这种情况下使用 Linq 查询方法:
我有 3 tables(用户、新闻和订阅)
1.1。在 Subscriptions
table 中必须存储新闻 ID,即用户订阅的新闻 ID(在用户 ID 旁边)
假定用户订阅时可以收到新闻通知,否则不会。
当我尝试检查用户是否订阅了该指定新闻时,我的问题就来了。我还需要 return 该人订阅的新闻(我基本上无法正确查询)。我尝试使用 Include()
方法,但我觉得我遗漏了一些东西。
这是我的代码:
数据上下文:
public class DataContext : IdentityDbContext<User, Role, string>
{
public DbSet<News> News { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
public DataContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Subscription>(s =>
{
s.HasKey(x => new
{
x.UserId,
x.NewsId
});
});
}
}
用户实体:
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Subscription> Subscriptions { get; set; }
}
News
实体:
public class News
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public ICollection<Subscription> Subscriptions { get; set; }
}
订阅实体:
public class Subscription
{
public string UserId { get; set; }
public string NewsId { get; set; }
public News News { get; set; }
public User User { get; set; }
}
NewsModel
View 中使用的模型(UserModel 包含与 User 相同的基本属性):
public class NewsModel
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public UserModel UserCreate { get; set; }
}
User
和News
是多对多的关系,其中Subscription
是类.
之间的连接
查询多对多关联的一般模式是
from news in context.News
from un in news.Subscriptions
let user = un.User
select ...
现在您可以 select
来自 news
和 user
的任何 属性 来在平面列表中显示来自 类 的数据。
select new NewsModel
{
Id = news.Id,
Title = news.Title,
Content = news.Content,
UserCreate = new UserModel
{
FirstName = user.FirstName,
LastName = user.LastName
}
}
旁注:我会使用像 NewsItem
这样的名称,更容易区分单复数名称。
我目前正在使用 Entity Framework Core 和 Net Core 2.1 网络应用程序学习如何指示用户是否订阅了指定新闻。
我花了一天的时间试图了解如何在这种情况下使用 Linq 查询方法:
我有 3 tables(用户、新闻和订阅)
1.1。在
Subscriptions
table 中必须存储新闻 ID,即用户订阅的新闻 ID(在用户 ID 旁边)假定用户订阅时可以收到新闻通知,否则不会。
当我尝试检查用户是否订阅了该指定新闻时,我的问题就来了。我还需要 return 该人订阅的新闻(我基本上无法正确查询)。我尝试使用 Include()
方法,但我觉得我遗漏了一些东西。
这是我的代码:
数据上下文:
public class DataContext : IdentityDbContext<User, Role, string>
{
public DbSet<News> News { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
public DataContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Subscription>(s =>
{
s.HasKey(x => new
{
x.UserId,
x.NewsId
});
});
}
}
用户实体:
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Subscription> Subscriptions { get; set; }
}
News
实体:
public class News
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public ICollection<Subscription> Subscriptions { get; set; }
}
订阅实体:
public class Subscription
{
public string UserId { get; set; }
public string NewsId { get; set; }
public News News { get; set; }
public User User { get; set; }
}
NewsModel
View 中使用的模型(UserModel 包含与 User 相同的基本属性):
public class NewsModel
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public UserModel UserCreate { get; set; }
}
User
和News
是多对多的关系,其中Subscription
是类.
查询多对多关联的一般模式是
from news in context.News
from un in news.Subscriptions
let user = un.User
select ...
现在您可以 select
来自 news
和 user
的任何 属性 来在平面列表中显示来自 类 的数据。
select new NewsModel
{
Id = news.Id,
Title = news.Title,
Content = news.Content,
UserCreate = new UserModel
{
FirstName = user.FirstName,
LastName = user.LastName
}
}
旁注:我会使用像 NewsItem
这样的名称,更容易区分单复数名称。