【问题标题】:Entity splitting with EF Code First issue使用 EF Code First 问题进行实体拆分
【发布时间】:2015-05-29 07:59:57
【问题描述】:

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

我有以下表格:

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

这会产生以下错误:

“组织”类型上的属性“Settings.AllowMultipleTimers” 无法映射,因为它已被明确排除在 模型或者它是 DbModelBuilderVersion 不支持的类型 正在使用。

我在这里做错了什么?

更新:我忘了提到我是手动创建数据库的,并且正在使用 CF fluent API 来映射我的模型,而不是使用“真正的”代码优先。

【问题讨论】:

  • 你先用真码吗?还是您在 SQL 中创建了表,然后添加了 ef 模型?
  • 对不起,我应该提到的是,我手动创建了表格,然后使用了流利的 api
  • 实体拆分不是用一个实体表示多个表吗?看起来每个表都有一个实体?
  • 为什么要将 AllowMultipleTimers 映射到表? AllowMultipleTimers 是布尔值而不是表。您需要将您的OrganizationSettings 映射为自己的实体,然后映射AllowMultipleTimes 是OrganizationSettings 实体的一个属性,并且OrganizationSettings 实体应该映射到表中。
  • 从建模的角度来看,OrganizationSettings 不是一个实体,它是一个值对象,是 Organization 实体的一部分。它作为技术细节存储在单独的表中。我只是想设置我的 EF 映射以尽可能地匹配模型。最后,我可能最终将它们视为单独的 EF 实体,并在我的存储库中将它们结合起来,但我很确定我过去有这种类型的映射工作,我只是不知道为什么它给了我那个错误现在。

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


【解决方案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);

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

【讨论】:

    【解决方案2】:

    您是否尝试在使用实体拆分的同时将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");
        });
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多