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 8615741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:27:16+00:00 2026-06-12T05:27:16+00:00

I’m having a nasty problem I really can’t understand. Basically I have a DbContext

  • 0

I’m having a nasty problem I really can’t understand.

Basically I have a DbContext defined as follows

public interface ISettingsContext : IDisposable 
{
    IDbSet<Site> Sites { get; }
    IDbSet<SettingGroup> Groups { get; }
    IDbSet<SettingProperty> Properties { get; }

    int SaveChanges();
}

public class SettingsContext : DbContext, ISettingsContext
{
    public SettingsContext(string connectionStringName) : base(connectionStringName)
    {
        Sites = Set<Site>();
        Groups = Set<SettingGroup>();
        Properties = Set<SettingProperty>();
    }

    public SettingsContext() : this("DefaultTestConnectionString") { }

    public IDbSet<Site> Sites { get; private set; }

    public IDbSet<SettingGroup> Groups { get; private set; }

    public IDbSet<SettingProperty> Properties { get; private set; }

    #region Model Creation

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        MapSite(modelBuilder.Entity<Site>());
        MapGroup(modelBuilder.Entity<SettingGroup>());
        MapProperty(modelBuilder.Entity<SettingProperty>());
        MapValue(modelBuilder.Entity<SiteSettingValue>());
        MapXmlValue(modelBuilder.ComplexType<XmlValue>());
        MapXmlType(modelBuilder.ComplexType<XmlTypeDescriptor>());
    }

    private void MapXmlType(ComplexTypeConfiguration<XmlTypeDescriptor> complexType)
    {
        complexType.Ignore(t => t.Type);
    }

    private void MapXmlValue(ComplexTypeConfiguration<XmlValue> complexType)
    {
        complexType.Ignore(t => t.Value);
    }

    private void MapValue(EntityTypeConfiguration<SiteSettingValue> entity)
    {
        entity.HasKey(k => new { k.PropertyId, k.SiteId }).ToTable("SiteValues", "settings");

        entity.Property(k => k.PropertyId).HasColumnName("FKPropertyID");

        entity.Property(k => k.SiteId).HasColumnName("FKSiteID");

        entity.Property(k => k.Value.RawData).HasColumnName("Value").IsMaxLength();

        entity.HasRequired(k => k.Site).WithMany(s => s.Settings).HasForeignKey(k => k.SiteId);

        entity.HasRequired(k => k.Property).WithMany().HasForeignKey(k => k.PropertyId);
    }

    private void MapProperty(EntityTypeConfiguration<SettingProperty> entity)
    {
        entity.HasKey(k => k.Id).ToTable("Properties", "settings");

        entity.Property(k => k.Id);

        entity.Property(k => k.Name);

        entity.Property(k => k.GroupId).HasColumnName("FKGroupID");

        entity.Property(k => k.PropertyDescriptor.RawData).HasColumnName("TypeDescriptor").IsMaxLength();

        entity.Property(k => k.DefaultValue.RawData).HasColumnName("DefaultValue").IsMaxLength();

        entity.HasRequired(k => k.Group).WithMany(k => k.Properties).HasForeignKey(k => k.GroupId);
    }

    private void MapGroup(EntityTypeConfiguration<SettingGroup> entity)
    {
        entity.HasKey(k => k.Id).ToTable("Groups", "settings");

        entity.Property(k => k.Id);

        entity.Property(k => k.Name);

        entity.HasMany(k => k.Properties).WithRequired(k => k.Group).HasForeignKey(k => k.GroupId);
    }

    private void MapSite(EntityTypeConfiguration<Site> entity)
    {
        entity.HasKey(k => k.Id).ToTable("Sites", "site");

        entity.Property(k => k.Id);

        entity.Property(k => k.Domain);
    }

    #endregion
}

Since I was carrying on some tests, I started working on this context without caring about IoC and DI, so I worked directly against the concrete class itself.

So I wrote this query:

public class SiteSettingsLoader : ILoader<SiteSettingsModel, int> {
...
var qSiteValues = from site in context.Sites
                  let settings = site.Settings
                  select new
                  {
                          SiteId = site.Id,
                          Settings = from value in settings
                                     join property in context.Properties on value.PropertyId equals property.Id into props
                                     from property in props
                                     select new
                                     {
                                             PropertyId = property.Id,
                                             Name = property.Name,
                                             GroupId = property.GroupId,
                                             Value = value.Value,
                                             CanBeInherited = property.CanBeInherited
                                     }
                  };

And everything worked just fine.
The problems started when I started passing the context as the interface it implements instead of its real type.

using (ISettingsContext context = new SettingsContext())
using (SiteSettingsLoader loader = new SiteSettingsLoader(context))
{
    SiteSettingsStore store = new SiteSettingsStore(loader);
    dynamic settings = store.GetItem(1);
}

This is the error

Unable to create a constant value of type
‘Settings.Entities.SettingProperty’. Only primitive types (‘such as
Int32, String, and Guid’) are supported in this context.

How is it possible? Did Microsoft play some dirty trick with reflection?

EDIT:
Both XmlValue and XmlTypeDescriptor are an implementation of the following interface that we use to automatically read the content of an XML field into a proper object.

public interface IXmlProperty
{
    string RawData { get; set; }
    void Load(string xml);
    XDocument ToXml();
}

Basically we map the content of the field to the RawData property; in its setters and getters we parse/generate the string containing the XML for the database.
I know it’s an ugly trick but it works and gets the job done.

  • 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-12T05:27:17+00:00Added an answer on June 12, 2026 at 5:27 am

    Based on this answer to a very similar question the reason for the problem seems to be that EF does not understand the properties of your custom interface type as queryable expressions and considers the context.Properties collection as a collection of constant objects of type SettingsProperty that you want to pass into the query. Using non-primitive constant objects is not supported by EF which causes your exception.

    Basically you need to use a (compile time) type that EF knows how to translate into a SQL fragment to incorporate into the whole query. That would be a DbContext.DbSet<T> itself or an IQueryable<T>:

    IQueryable<SettingProperty> properties = context.Properties;
    var qSiteValues = from site in context.Sites
                      let settings = site.Settings
                      select new
                      {
                          SiteId = site.Id,
                          Settings = from value in settings
                                     join property in properties
                                        on value.PropertyId equals property.Id
                                        into props
                                     from property in props
                                     select new
                                     {
                                         PropertyId = property.Id,
                                         Name = property.Name,
                                         GroupId = property.GroupId,
                                         Value = value.Value,
                                         CanBeInherited = property.CanBeInherited
                                     }
                  };
    

    It might be necessary to use the same trick for context.Sites but I am not sure.

    (If the above works don’t forget to upvote Eranga’s linked answer because it’s awesome.)

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.