我正在尝试在基于表的模型(Driver
)和基于SQL查询的模型(DriverSchedule
)之间建立一对多关系。
我很难使这种体系结构起作用。
Driver
模型:
[Table("Drivers")]
public class Driver
{
[Key]
[Column("DriverKey")]
public int ID { get; set; }
...
public virtual ICollection<DriverSchedule> DriverSchedules { get; set; }
}
DriverSchedule
模型:
public class DriverSchedule
{
public int DriverID { get; set; } // foreign key
public DateTime Date { get; set; }
public bool IsScheduled { get; set; }
public virtual Driver Driver { get; set; }
}
VendorDbContext
上下文:
public class VendorDbContext : DbContext
{
...
public DbSet<Driver> Drivers { get; set; }
...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DriverSchedule>().HasKey(table => new { table.DriverID, table.Date });
modelBuilder.Entity<Driver>().HasMany(d => d.DriverSchedules).WithOne(s => s.Driver);
...
modelBuilder.Query<DriverSchedule>();
}
public IQueryable<DriverSchedule> DriverSchedules(DateTime startingDate, DateTime endingDate) =>
Query<DriverSchedule>().FromSql("<SQL>");
}
查看:
@model IEnumerable<Driver>
...
@foreach (var item in Model)
{
<tr>
<td>@(item.Name)</td>
<td>@(item.ID)</td>
<td><ul>
foreach (var driverSchedule in item.DriverSchedules)
{
<li>@(driverSchedule.ToString("MM/dd/yy") - @(driverSchedule.IsScheduled)</li>
}
</ul></td>
</tr>
}
...
我的LINQ查询:
var model = await (
from driver in _context.Drivers
join schedule in _context.DriverSchedules(startDate.Date, endDate.Date) on driver.ID equals schedule.DriverID
orderby driver.LastName, driver.FirstName
select driver
).Include(x => x.DriverSchedules)
.ToListAsync();
运行代码时,此行:
modelBuilder.Query<DriverSchedule>();
产生一个错误:
无法将查询类型'DriverSchedule'添加到模型中,因为 具有相同名称的实体类型已经存在。
**编辑0 **
此帖子https://msdn.microsoft.com/en-us/magazine/mt847184.aspx提示我需要
从Driver
删除导航属性:
//public virtual ICollection<DriverSchedule> DriverSchedules { get; set; }
从DriverSchedule
删除导航属性:
//public virtual Driver Driver { get; set; }
从OnModelCreating
中删除关系定义:
// modelBuilder.Entity<DriverSchedule>().HasKey(table => new { table.DriverID, table.Date });
// modelBuilder.Entity<Driver>().HasMany(d => d.DriverSchedules).WithOne(s => s.Driver);
更改查询界面:
IQueryable<DriverSchedule> DriverSchedules(DateTime startingDate, DateTime endingDate) =>
Query<DriverSchedule>() ...
定义查询和实体之间的关系:
modelBuilder.Query<DriverSchedule>().HasOne<Driver>().WithMany();
然后重新编写查询:
model = await _context.DriverSchedules(startDate.Date, endDate.Date)
.Include("Driver")
.ToListAsync();
哪个会因以下错误而失败:
属性“ Driver”不是实体类型的导航属性 “ DriverSchedule”。 'Include(string)'方法只能与 '。'导航属性名称的分隔列表。
** / edit 0 **
**编辑1 **
SELECT m.DriverID, c.Date
,CAST( CASE WHEN COUNT(CASE WHEN c.date <> CAST(m.StartTime AS DATE) THEN NULL ELSE m.MovementID end)>0 THEN 1 ELSE 0 END AS BIT) IsScheduled
FROM dbo.Calendar c
CROSS APPLY vMovements m
WHERE 1 = 1
AND c.date BETWEEN {startingDate} AND {endingDate}
AND m.DriverID IS NOT NULL
GROUP BY c.date, m.DriverID
** / edit 1 **
我想念什么?
答案 0 :(得分:1)
那是因为您已经有一个名为 DriverSchedule
的实体问题在这里:
modelBuilder.Entity<DriverSchedule>();
modelBuilder.Query<DriverSchedule>(); // wrong.
创建另一个模型,以便在DriverSchedules方法中获取和映射数据结果。所以:
public class Driversched
{
public int DriverID { get; set; } // foreign key
public DateTime Date { get; set; }
public bool IsScheduled { get; set; }
public virtual Driver Driver { get; set; }
}
然后:
modelBuilder.Query<Driversched>();
最后:
public IQueryable<Driversched> DriverSchedules(DateTime startingDate, DateTime endingDate) =>
Query<DriverSchedule>().FromSql("<SQL>");
建议,切勿将实体类用作模型。