实体框架一对多单边连接

时间:2013-02-05 14:34:57

标签: linq-to-entities ef-code-first entity-framework-5 foreign-key-relationship

我有这两个表:

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; }
}

我的问题是使用流畅的API定义关系,因为您可以看到从Logging到LoggingLevel有一个外键,但我不想在LoggingLevel类上有相应的日志集合。我尝试了以下几种组合,包括.WithRequiredDependant.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'

我在查询中的错误字段周围放置**,但粗体在代码部分不起作用。

以下是我完整的流畅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();

1 个答案:

答案 0 :(得分:2)

嗯,你想建立一对多的关系吧?

一个LoggingLevel可以有多个Logging,一个Logging需要一个LoggingLevel

您配置关系的方式将使EF将其创建为一对一。

要使其正确成为一对多,您必须拨打WithMany而不是WithOptional

HasRequired(l => l.Level).WithMany().Map(p => p.MapKey("LogLevel"))

由于用作外键的列在数据库中称为LogLevel,因此必须使用Map方法告知EF,并使用正确的列名称调用MapKey方法。

否则按惯例生成列名。它使用导航属性名称和目标类型的键属性的名称,并附加值。

在您的情况下,这会导致Level_LoggingLevelName,这是不对的。因此,您必须手动映射列名称。