在视图之外创建 DbQuery table
Create DbQuery out of View table
我正在尝试从 SQL 视图创建一个 ExtendedStudent 的 DbQuery,该视图由 2 个不同的表构成(参见下面的代码 SQL)。
我看过以下帖子:
Entity Framework Core Query Types And EF Core 2.1 Query Types
都用过里面有导航属性的模型,然后成功从Fluent FluentAPI中取到。
但是当我也尝试这样做时,出现异常,例如“无效的列名 'PrefixId1'
我使用的型号是:
public class ExtendedStudent {
public int IdNumber {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public virtual Prefix Prefix {get; set;}
public int Score {get; set;}
}
public class Prefix {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Id {get ;set;}
[required]
public string Name {get; set;}
}
applicationDbContext.cs 文件是:
public class ApplciationDbContext : DbContext{
DbSet<Prefix> Prefixes {get; set;}
DbQuery<ExtendedStudent> ExtendedStudents {get ;set;}
...
protected override void OnModelCreating(ModelBuilder builder) {
builder.Query<ExtendedStudent>.ToView("ExtendedStudent");
builder.Query<ExtendedStudent>.HasOne<Prefix>().WithMany();
}
}
最后,我尝试着这样去取数据
var students = applciationDbContext.ExtendedStudents.Include(v => v.Prefix).ToList();
我在 SQL 中创建了 ExtendedStudents 视图,如下所示:
CREATE VIEW [Organization].[ExtendedStudent] AS
SELECT [TableA].[Student].[FirstName]
,[TableA].[Student].[LastName]
,[TableA].[Student].[PrefixId]
,[TableA].[Student].[IdNumber]
,[Evaluation].[Student].[Score]
FROM [TableA].[Student] AS [Students]
INNER JOIN [Evaluation].[Student] ON [Evaluation].[Student].StudentId = [TableA].[Student].[IdNumber]
我试过向 ExtendedStudent 添加 PrefixId 属性,或添加外键,但没有任何效果。
我收到一条错误消息
"An exception of type 'System.Data.SqlClient.SqlException' occured in Microsoft.EntityFrameworkCore.dll but was not handled in user code: 'Invalid column name 'PrefixId1'.'
这里
builder.Query<ExtendedStudent>.HasOne<Prefix>().WithMany();
with .HasOne<Prefix>()
你告诉 EF Core 在每一端创建多对一关系 而没有 导航 属性。
但是导航 属性 ExtendedStudent.Prefix
已经暗示了关系,因此 EF Core 假定具有默认 FK 属性 和列名的 second 关系PrefixId1
(因为 PrefixId
已被导航 属性 隐含的 "other" 关系使用。
要解决此问题,请将导航 属性 传递给关系配置:
builder.Query<ExtendedStudent>.HasOne(e => e.Prefix).WithMany();
我正在尝试从 SQL 视图创建一个 ExtendedStudent 的 DbQuery,该视图由 2 个不同的表构成(参见下面的代码 SQL)。
我看过以下帖子:
Entity Framework Core Query Types And EF Core 2.1 Query Types 都用过里面有导航属性的模型,然后成功从Fluent FluentAPI中取到。 但是当我也尝试这样做时,出现异常,例如“无效的列名 'PrefixId1'
我使用的型号是:
public class ExtendedStudent {
public int IdNumber {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public virtual Prefix Prefix {get; set;}
public int Score {get; set;}
}
public class Prefix {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Id {get ;set;}
[required]
public string Name {get; set;}
}
applicationDbContext.cs 文件是:
public class ApplciationDbContext : DbContext{
DbSet<Prefix> Prefixes {get; set;}
DbQuery<ExtendedStudent> ExtendedStudents {get ;set;}
...
protected override void OnModelCreating(ModelBuilder builder) {
builder.Query<ExtendedStudent>.ToView("ExtendedStudent");
builder.Query<ExtendedStudent>.HasOne<Prefix>().WithMany();
}
}
最后,我尝试着这样去取数据
var students = applciationDbContext.ExtendedStudents.Include(v => v.Prefix).ToList();
我在 SQL 中创建了 ExtendedStudents 视图,如下所示:
CREATE VIEW [Organization].[ExtendedStudent] AS
SELECT [TableA].[Student].[FirstName]
,[TableA].[Student].[LastName]
,[TableA].[Student].[PrefixId]
,[TableA].[Student].[IdNumber]
,[Evaluation].[Student].[Score]
FROM [TableA].[Student] AS [Students]
INNER JOIN [Evaluation].[Student] ON [Evaluation].[Student].StudentId = [TableA].[Student].[IdNumber]
我试过向 ExtendedStudent 添加 PrefixId 属性,或添加外键,但没有任何效果。
我收到一条错误消息
"An exception of type 'System.Data.SqlClient.SqlException' occured in Microsoft.EntityFrameworkCore.dll but was not handled in user code: 'Invalid column name 'PrefixId1'.'
这里
builder.Query<ExtendedStudent>.HasOne<Prefix>().WithMany();
with .HasOne<Prefix>()
你告诉 EF Core 在每一端创建多对一关系 而没有 导航 属性。
但是导航 属性 ExtendedStudent.Prefix
已经暗示了关系,因此 EF Core 假定具有默认 FK 属性 和列名的 second 关系PrefixId1
(因为 PrefixId
已被导航 属性 隐含的 "other" 关系使用。
要解决此问题,请将导航 属性 传递给关系配置:
builder.Query<ExtendedStudent>.HasOne(e => e.Prefix).WithMany();