在 .NET Core 中是否类似于 Ruby on Rails 中的 ActiveRecord?

Is in .NET Core something similar to ActiveRecord in Ruby on Rails?

我正在从 Rails 切换到 .NET Core,但我真正想念的是 ActiveRecord ORM。在模型中,您只需定义关系:

#Town Model
class Town < ApplicationRecord
  belongs_to :country
end

#Country Model
class Country < ApplicationRecord
  has_many :towns
end

然后您可以简单地获取特定国家/地区所有城镇的列表:

@country = Country.find(params[:id])
@towns = @country.towns

这很清楚,尤其是在您通过 ID 链接多个模型的情况下。在 .Net Core 中,我通常以这样的方式结束:

Task<List<Town>> towns = await _context.Towns.Where(x => x.CountryId == countryId).ToListAsync();

这仍然可以接受,但这只是因为只有模型-模型关系。

假设我们想要获得 Rails 中所选城镇的大陆:

@town = Town.find(params[:id])
@continent = @town.country.continent

在 .NET Core 中,现在我必须使用连接,最终结果会非常复杂,这里很容易出错(甚至不复杂的情况)。具有非常复杂 SQL 查询的图像,LINQ 对您帮助不大,您非常接近编写纯 SQL.

这就是我问的原因,如果在 .NET Core 中有类似于 Rails 中非常方便的 ActiveRecord ORM 的东西。

感谢您的回复和时间。

EF 支持开箱即用的导航属性,因此在简单情况下您不需要联接。如果你愿意,你也可以使用延迟加载。查看更多信息 here, here and here 例如。

在您的情况下,如果您正确设置了实体之间的关系,您就可以翻译

@country = Country.find(params[:id])
@towns = @country.towns

进入:

var country = await _context.Country.Include(c => c.Towns).FindAsync(countryId); 
// or await _context.Country.Include(c => c.Towns).FirstAsync(c => c.Id == countryId) 
var towns = country.Towns;

或启用延迟加载:

var country = await _context.Country.FindAsync(countryId); 
var towns = country.Towns;

第二个片段:

@town = Town.find(params[:id])
@continent = @town.country.continent

可以翻译成:

var town = await _context.Town
     .Include(t => t.Country)
     .ThenInclude(c => c.Continent)
     .FindAsync(TownId); 
var country = town.Country.Continent;