Entity splitting with EF Code First issue

c# ef-code-first entity-framework entity-framework-6

Question

So I am trying to achieve entity splitting in EF 6.1 with Code First, and I am running into an error.

I have the following tables:

CREATE TABLE [dbo].[Organization]
(
    [OrganizationId] INT NOT NULL PRIMARY KEY IDENTITY, 
    [TenantId] INT NOT NULL, 
    [Name] NVARCHAR(80) NOT NULL
)

CREATE TABLE [dbo].[OrganizationSettings]
(
    [OrganizationSettingsId] INT NOT NULL PRIMARY KEY IDENTITY, 
    [OrganizationId] INT NOT NULL, 
    [AllowMultipleTimers] BIT NOT NULL, 
    CONSTRAINT [FK_OrganizationSettings_Organization] FOREIGN KEY (OrganizationId) REFERENCES Organization(OrganizationId)
)

With the following model objects:

public partial class Organization
{
    public int OrganizationId { get; set; }
    public int TenantId { get; set; }
    public string Name { get; set; }

    public OrganizationSettings Settings { get; set; }
}

public class OrganizationSettings
{
    public int OrganizationSettingsId { get; set; }
    public int OrganizationId { get; set; }
    public bool AllowMultipleTimers { get; set; }
}

With the following config code:

        var org = modelBuilder.Entity<Organization>();
        org.Map(u =>
        {
            u.Properties(m => new { m.TenantId, m.Name });
        })
        .ToTable("Organization");

        org.Map(u =>
        {
            u.Property(m => m.Settings.AllowMultipleTimers).HasColumnName("AllowMultipleTimers");
            u.ToTable("OrganizationSettings");
        });

Then just the following query:

context.Organizations.FirstOrDefault();

Which yields the following error:

The property 'Settings.AllowMultipleTimers' on type 'Organization' cannot be mapped because it has been explicitly excluded from the model or it is of a type not supported by the DbModelBuilderVersion being used.

What am I doing wrong here?

Update: I forgot to mention that I created the database by hand, and am using the CF fluent API to map my models, rather than using "real" Code First.

1
0
5/28/2015 4:47:59 PM

Popular Answer

While I was pretty sure I had this mapping working before, I went ahead and went a little different route.

First I got rid of the surrogate key on `OrganizationSettings (probably not strictly necessary), and then mapped it as an entity with a 1:1 relationship.

My OrganizationSettings is now:

public class OrganizationSettings
{
    public int OrganizationId { get; set; }
    public bool AllowMultipleTimers { get; set; }
}

OrganizationId is both a primary key and a foreign key.

And the config is:

        var org = modelBuilder.Entity<Organization>()
            .Map(u =>
            {
                u.Properties(m => new { m.TenantId, m.Name });
            })
            .HasRequired(m => m.Settings)
            .WithRequiredPrincipal();


        modelBuilder.Entity<OrganizationSettings>()
            .HasKey(m => m.OrganizationId);

And this seems to work just fine. Since I'm not exposing a DbSet for OrganizationSettings it keeps the conceptual modeling of OrganizationSettings as a value object intact.

1
5/28/2015 9:44:54 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow