我正在努力使用 Entity Framework Core 创建一个不可为空/必需的拥有类型。我正在对 PostgreSQL 数据库使用 EF Core 3.0。
我的价值对象:
public class PersonName
{
public PersonName(string name)
{
this.Name = name;
}
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的实体:
public class Person
{
public int Id { get; set; }
public virtual PersonName FullName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的实体配置:
public void Configure(EntityTypeBuilder<Person> builder)
{
builder.ToTable(nameof(Person));
builder.HasKey(person => person.Id);
builder.OwnsOne(person => person.FullName, personName =>
{
personName.Property(pn => pn.Name).IsRequired().HasColumnName("FullName");
});
}
Run Code Online (Sandbox Code Playgroud)
值类型属性已成功保存到数据库中的“Person”表中,但尽管我使用的是“IsRequired()”方法,但该列仍然可以为空。
非常感谢!
c# postgresql value-objects entity-framework-core owned-types
我在使用 EF Core 3.1 查询 PostgreSQL 数据库时遇到问题。
查询很简单
var gamesQuery = this.dbContext.Games.Where(game => game.StartTime > DateTime.Now).AsQueryable();
// 'request.TimeFrom' is of type System.TimeSpan and the value is populated
gamesQuery = gamesQuery.Where(game => game.StartTime.TimeOfDay >= request.TimeFrom);
// .ToList()-int here causes the exception.
var games = gamesQuery.ToList();
Run Code Online (Sandbox Code Playgroud)
异常消息明确指出查询无法翻译:
“无法翻译 LINQ 表达式 'DbSet\r\n .Where(g => g.StartTime > DateTime.Now)\r\n .Where(g => g.StartTime.TimeOfDay >= __request_TimeFrom_0)'。以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用来显式切换到客户端评估。请参阅https://go.microsoft.com /fwlink/?linkid=2101038了解更多信息。”
问题是相同的查询在 .NET Core 2.2 中运行良好。我还没有发现任何有关该问题的信息。
有人知道这个的原因是什么,还是我错过了什么?