我有这两张表:
CREATE TABLE [MIS].[Logging](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MachineName] [nvarchar](255) NULL,
[LogSource] [nvarchar](255) NOT NULL,
[LogSourceVersion] [nvarchar](255) NULL,
[LogLevel] [nvarchar](255) NOT NULL,
[LogMessage] [nvarchar](max) NOT NULL,
[LogDetailLocation] [nvarchar](255) NULL,
[LogTime] [datetime] NOT NULL,
CONSTRAINT [PK_Logging] PRIMARY KEY CLUSTERED
(
[Id] ASC
))
CREATE TABLE [MIS].[LoggingLevels](
[LoggingLevelName] [nvarchar](255) NOT NULL,
[LoggingLevelValue] [int] NULL,
CONSTRAINT [PK_LoggingLevels] PRIMARY KEY CLUSTERED
(
[LoggingLevelName] ASC
))
而这两个表类:
public class Logging
{
public int Id { get; set; }
public string MachineName { get; set; }
public string LogSource { get; set; }
public string LogSourceVersion { get; set; }
public string LogDetailLocation { get; set; }
public DateTime LogTime { get; set; }
public string LogMessage { get; set; }
//public string LogLevel { get; set; }
public LoggingLevel Level { get; set; }
}
public class LoggingLevel
{
public int LoggingLevelValue { get; set; }
public string LoggingLevelName { get; set; }
}
我的问题是使用 fluent API 定义关系,您可以看到从 Logging 到 LoggingLevel 的外键,但我不希望 LoggingLevel 类上的相应日志集合。我尝试了以下几种组合,包括.WithRequiredDependant
and .WithRequiredPrincipal
:
modelBuilder.Entity<Logging>()
.HasRequired(l => l.Level)
.WithOptional()
.Map(l => l.MapKey("LoggingLevelName"));
这永远不会使连接正确,这是来自 LoggingLevel 表的数据,以及来自日志记录表的一些示例行。
LoggingLevelName LoggingLevelValue
---------------- -----------------
ALL -2147483648
DEBUG 30000
INFO 40000
WARN 60000
ERROR 70000
FATAL 110000
OFF 2147483647
id MachineName LogSource LogSourceVersion LogLevel LogMessage LogDetailLocation LogTime
----------- ----------- --------- ---------------- ---------- ---------- ----------------- -----------------------
115170694 redacted redacted 17 INFO redacted redacted 2013-01-29 04:00:02.420
115170695 redacted redacted (null) INFO redacted redacted 2013-01-29 04:00:03.587
115170696 redacted redacted (null) INFO redacted redacted 2013-01-29 04:01:01.357
115170697 redacted redacted NULL INFO redacted redacted 2013-01-29 04:01:01.357
115170698 redacted redacted 10 INFO redacted redacted 2013-01-29 04:01:01.933
115170699 redacted redacted 17 INFO redacted redacted 2013-01-29 04:01:33.320
115170700 redacted redacted (null) INFO redacted redacted 2013-01-29 04:02:29.990
115170701 redacted redacted (null) INFO redacted redacted 2013-01-29 04:02:30.000
115170702 redacted redacted (null) INFO redacted redacted 2013-01-29 04:02:30.040
115170703 redacted redacted (null) INFO redacted redacted 2013-01-29 04:02:30.243
这是我正在尝试运行的示例 linq 查询:
var results = (from log in db.Loggings
where log.Level.LoggingLevelValue >= 60000
&& log.LogTime >= filterDate
orderby log.LogTime descending
select log);
return results.ToList();
通过使用更新结果:
modelBuilder.Entity<Logging>()
.HasRequired(l => l.Level).WithMany();
这是运行我的查询时生成的 SQL:
exec sp_executesql N'SELECT
[Project1].[Id] AS [Id],
[Project1].[MachineName] AS [MachineName],
[Project1].[LogSource] AS [LogSource],
[Project1].[LogSourceVersion] AS [LogSourceVersion],
[Project1].[LogDetailLocation] AS [LogDetailLocation],
[Project1].[LogTime] AS [LogTime],
[Project1].[LogMessage] AS [LogMessage],
[Project1].**[Level_LoggingLevelName]** AS [Level_LoggingLevelName]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[MachineName] AS [MachineName],
[Extent1].[LogSource] AS [LogSource],
[Extent1].[LogSourceVersion] AS [LogSourceVersion],
[Extent1].[LogDetailLocation] AS [LogDetailLocation],
[Extent1].[LogTime] AS [LogTime],
[Extent1].[LogMessage] AS [LogMessage],
[Extent1].**[Level_LoggingLevelName]** AS [Level_LoggingLevelName]
FROM [MIS].[Logging] AS [Extent1]
INNER JOIN [MIS].[LoggingLevels] AS [Extent2] ON [Extent1].[Level_LoggingLevelName] = [Extent2].[LoggingLevelName]
WHERE ([Extent2].[LoggingLevelValue] >= @p__linq__0) AND ( CAST( [Extent1].[LogTime] AS datetime2) >= @p__linq__1)
) AS [Project1]
ORDER BY [Project1].[LogTime] DESC',N'@p__linq__0 int,@p__linq__1 datetime2(7)',@p__linq__0=60000,@p__linq__1='2013-02-04 00:00:00'
我在查询中将 ** 放在错误的字段周围,但粗体在代码部分中不起作用。
这是我完整的 fluent API 配置:
modelBuilder.Entity<Logging>().ToTable("Logging", "MIS");
modelBuilder.Entity<Logging>().HasKey(l => l.Id);
modelBuilder.Entity<LoggingLevel>().ToTable("LoggingLevels", "MIS");
modelBuilder.Entity<LoggingLevel>().HasKey(ll => ll.LoggingLevelName);
modelBuilder.Entity<ProblemResolution>().ToTable("ProblemResolutions", "MIS");
modelBuilder.Entity<ProblemResolution>().HasKey(r => r.Id);
modelBuilder.Entity<Logging>()
.HasRequired(l => l.Level).WithMany();