实体分割与EF代码第一个问题

本文关键字:第一个 问题 代码 EF 分割 实体 | 更新日期: 2023-09-27 17:51:04

所以我试图在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();

产生以下错误:

属性"设置"。AllowMultipleTimers' on type 'Organization'类型中显式地排除了它,因此无法映射或者它是DbModelBuilderVersion不支持的类型被使用。

我在这里做错了什么?

更新:我忘了说我手工创建了数据库,并且使用CF流畅API来映射我的模型,而不是使用"真正的"代码优先。

实体分割与EF代码第一个问题

虽然我很确定我之前已经完成了这个映射,但我还是走了一条稍微不同的路线。

首先,我去掉了' OrganizationSettings '上的代理键(可能不是严格必要的),然后将其映射为具有1:1关系的实体。

My OrganizationSettings is now:

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

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

您是否试图将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");
    });