无法映射“Company”属性“Demand.Company”,因为数据库提供程序不支持此类型

问题描述 投票:0回答:1

我正在为我的模型创建类映射,但当应用程序尝试迁移数据库时,这种类型的错误不断发生。我最初认为问题是表之间的关系缺少影子属性,但即使在创建和配置它们之后,它仍然存在。

这是我的模型和各自的映射:

公司模型

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

需求模型

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

        // Shadow Properties
        public virtual int CompanyId { get; set; }
        public virtual int ResponsibleId { get; set; }
    }
}

公司地图

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

需求图

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. See https://aka.ms/efcore-docs-value-converters for more information. Alternately, exclude the property from the model using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
。知道我做错了什么吗?

c# entity-framework
1个回答
0
投票

非常确定

builder.HasOne<Company>()
无效,它应该看起来像:

 builder.HasOne(x => x.Company)
            .WithMany(c => c.Demands)
            .HasForeignKey(x => x.CompanyId)
            .IsRequired();
© www.soinside.com 2019 - 2024. All rights reserved.