Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:37:13+00:00 2026-06-18T08:37:13+00:00

I have these two tables: CREATE TABLE [MIS].[Logging]( [Id] [int] IDENTITY(1,1) NOT NULL, [MachineName]

  • 0

I have these two tables:

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
)) 

And These two classes for the tables:

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

My problem is defining the relationship using the fluent API, as you can see there is a foreign key from Logging to LoggingLevel, but I don’t want the corresponding collection of logs on the LoggingLevel class. I’ve tried a number of combinations of the following, including .WithRequiredDependant and .WithRequiredPrincipal:

    modelBuilder.Entity<Logging>()
                .HasRequired(l => l.Level)
                .WithOptional()
                .Map(l => l.MapKey("LoggingLevelName"));

This never ends up getting the join correct, here is data from the LoggingLevel table, and some sample rows from the logging table.

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

Here is sample linq query I’m trying to run:

    var results = (from log in db.Loggings
                   where log.Level.LoggingLevelValue >= 60000
                         && log.LogTime >= filterDate
                   orderby log.LogTime descending
                   select log);
    return results.ToList();

Update results from using :

    modelBuilder.Entity<Logging>()
                .HasRequired(l => l.Level).WithMany();

Here is the SQL this generates when running my query:

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'

I put ** around the fields in error in the query, bold doesn’t work in code sections though.

Here is my full fluent API configuration:

    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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:37:14+00:00Added an answer on June 18, 2026 at 8:37 am

    Well, you want to create a one-to-many relationship, right?

    One LoggingLevel can have many Logging and one Logging is required to have one LoggingLevel.

    The way you are configuring the relationship, will make EF create it as a one-to-one.

    To make it correctly become one-to-many, you have to call WithMany instead of WithOptional:

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

    Since the column used as the foreign key is called LogLevel in the database, you have to tell EF by using the Map method, and call the MapKey method with the correct column name.

    Otherwise it generates the column name by convention. It uses the navigation property name and the name of the key property of the target type, and appends the values.

    In your case this results in Level_LoggingLevelName, which is not right. So you have to map the column name manually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables like these: CREATE TABLE people ( id INT NOT NULL,
I have two tables: CREATE TABLE [dbo].[Task]( [SysTask] [int] IDENTITY(1,1) NOT NULL, [TaskStatus] [int]
I have these two tables: CREATE TABLE x ( id INT NOT NULL, exclude
I have two tables: CREATE TABLE [NEWS] ( [ID] INT IDENTITY(1,1) NOT NULL, [TITLE]
I have these two tables in mysql: create table a ( id INT NOT
two tables, created using these scripts: create table user_auth ( username varchar(255) NOT NULL,
I have two tables: create table dbo.Dates ( Id int not null constraint Dates_Id_PK
I have two tables: CREATE TABLE 'sales_sheet' ( `_id` int(11) NOT NULL AUTO_INCREMENT, `_typed_by`
I have two tables: CREATE TABLE dbo.country ( cntry_id VARCHAR(2) NOT NULL, name VARCHAR(50)
I have two tables: CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.