You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EF Core数据库迁移报错:Demand.Company属性无法映射,求排查

问题:EF Core数据库迁移时类型映射错误

我正在为模型创建类映射,但应用程序执行数据库迁移时持续出现类型错误。原本以为是表间关系缺少影子属性,但创建并配置后问题仍存在。

模型代码

Company 模型

namespace Api.Domain.Model
{
    public class Company : BaseTable
    {
        public virtual int Id { get; protected set; }
        public virtual string Name { get; set; }
        public virtual string Cnpj { get; set; }
        public virtual string Email { get; set; }
        public virtual string Description { get; set; }
        public virtual IEnumerable<Demand> Demands { get; set; }
    }
}

Demand 模型

namespace Api.Domain.Model
{
    public class Demand : BaseTable
    {
        public virtual int Id { get; protected set; }
        public virtual string Title { get; set; }
        public virtual string Description { get; set; }
        public virtual Company Company { get; set; }
        public virtual string Department { get; set; }
        public virtual string Benefits { get; set; }
        public virtual string Details { get; set; }
        public virtual string Restrictions { get; set; }
        public virtual string Keywords { get; set; }
        public virtual Person Responsible { get; set; }
        public virtual IEnumerable<Match> Matches { get; set; }

        // 显式外键字段(非影子属性)
        public virtual int CompanyId { get; set; }
        public virtual int ResponsibleId { get; set; }
    }
}

映射代码

Company 映射

namespace Api.Domain.Map
{
    public class CompanyMap : BaseMap<Company>
    {
        protected override void Configure(EntityTypeBuilder<Company> builder)
        {
            // Primary Key
            builder.HasKey(x => x.Id);

            // Properties
            builder.Property(x => x.Id).UseIdentityColumn();
            builder.Property(x => x.Name).IsRequired().HasMaxLength(255);
            builder.Property(x => x.Cnpj).IsRequired().HasMaxLength(14);
            builder.Property(x => x.Email).IsRequired().HasMaxLength(255);
            builder.Property(x => x.Description).HasMaxLength(1000);

            // Relationships
            builder.Navigation(x => x.Demands);

            // Indexes
            builder.HasIndex(x => x.Cnpj).IsUnique();
            builder.HasIndex(x => x.Email).IsUnique();
        }
    }
}

Demand 映射

namespace Api.Domain.Map
{
    public class DemandMap : BaseMap<Demand>
    {
        protected override void Configure(EntityTypeBuilder<Demand> builder)
        {
            // Primary Key
            builder.HasKey(x => x.Id);

            // Properties
            builder.Property(x => x.Id).UseIdentityColumn();
            builder.Property(x => x.Title).IsRequired().HasMaxLength(255);
            builder.Property(x => x.Description).IsRequired().HasMaxLength(1000);
            builder.Property(x => x.Department).HasMaxLength(255);
            builder.Property(x => x.Benefits).HasMaxLength(1000);
            builder.Property(x => x.Details).HasMaxLength(1000);
            builder.Property(x => x.Restrictions).HasMaxLength(1000);
            builder.Property(x => x.Keywords).HasMaxLength(255);

            // Relationships
            builder.Navigation(x => x.Matches);

            builder.HasOne<Company>()
                .WithMany(c => c.Demands)
                .HasForeignKey(x => x.CompanyId)
                .IsRequired();

            builder.HasOne<Person>()
                .WithMany()
                .HasForeignKey(x => x.ResponsibleId)
                .IsRequired();

            // Indexes
            builder.HasIndex(x => x.Company);
            builder.HasIndex(x => x.Responsible);
        }
    }
}

报错信息

The 'Company' property 'Demand.Company' could not be mapped because the database provider does not support this type. Consider converting the property value to a type supported by the database using a value converter. Alternately, exclude the property from the model using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.


解决方案

错误出在两个关键地方:

  • 索引配置错误:不能直接对导航属性Company、Responsible创建索引,数据库只支持对字段建索引,需替换为对应的外键字段:

    // 替换原索引代码
    builder.HasIndex(x => x.CompanyId);
    builder.HasIndex(x => x.ResponsibleId);
    
  • 导航属性关联缺失:配置HasOne关系时未指定实体类中的导航属性,导致EF Core无法将Demand.Company与外键CompanyId关联。修改关系配置:

    // 替换原Company关系配置
    builder.HasOne(d => d.Company)
        .WithMany(c => c.Demands)
        .HasForeignKey(d => d.CompanyId)
        .IsRequired();
    
    // 替换原Person关系配置
    builder.HasOne(d => d.Responsible)
        .WithMany()
        .HasForeignKey(d => d.ResponsibleId)
        .IsRequired();
    

另外注意:你在Demand模型中声明的CompanyId和ResponsibleId是显式外键字段,不是影子属性。影子属性是不在实体类中声明、仅存在于EF Core模型的字段,若要使用影子属性,需删除实体类中的这两个属性,再通过builder.Property<int>("CompanyId")配置。

修改后重新生成迁移即可解决问题。


内容的提问来源于stack exchange,提问作者Ayrton Albuquerque

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:02:06