实体拆分与EF Code First问题

时间:2015-05-28 16:11:42

标签: c# entity-framework ef-code-first entity-framework-6

所以我尝试使用Code First在EF 6.1中实现实体拆分,并且我遇到了错误。

我有以下表格:

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

使用以下模型对象:

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

使用以下配置代码:

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

然后只需以下查询:

context.Organizations.FirstOrDefault();

产生以下错误:

  

属性&#39; Settings.AllowMultipleTimers&#39;关于类型&#39;组织&#39;   无法映射,因为它已被明确排除   model或它是DbModelBuilderVersion不支持的类型   被使用。

我在这里做错了什么?

更新:我忘了提到我手动创建了数据库,并使用CF流畅的API来映射我的模型,而不是使用&#34;真正的&#34;代码优先。

2 个答案:

答案 0 :(得分:1)

虽然我很确定之前有这种映射工作,但我继续前进并走了一条不同的路线。

首先,我删除了`OrganizationSettings上的代理键(可能不是绝对必要的),然后将其映射为具有1:1关系的实体。

我的组织设置现在是:

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

OrganizationId既是主键又是外键。

配置是:

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

这似乎工作得很好。由于我没有为DbSet公开OrganizationSettings,因此它将OrganizationSettings的概念建模保持为完整的值对象。

答案 1 :(得分:0)

您是否尝试在使用实体拆分时将OrganizationSettings设置为复杂类型?这样的事情,也许是:

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 bool AllowMultipleTimers { get; set; }
}
// if you don't have a key defined on OrganizationSettings, this might not be needed
modelBuilder.ComplexType<OrganizationSettings>();

modelBuilder.Entity<Organization>()
    .Map(u =>
    {
        u.Properties(m => new { m.OrganizationId, m.TenantId, m.Name });
        u.ToTable("Organization");
    })
    .Map(u =>
    {
        u.Properties(m => new { m.OrganizationId, m.Settings.AllowMultipleTimers });
        u.ToTable("OrganizationSettings");

        // If you wanted to set the key column name
        u.Property(m => m.OrganizationId).HasColumnName("OrganizationSettingsId");
    });