EF .net6 迁移“必须是非接口引用类型才能用作实体类型。”

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

所以我正在尝试迁移我的代码, 我使用 C# 和 .Net 6 以及干净的架构, 我在“核心”解决方案中有这个:

在实体文件夹中

public class Partenaire : AuditableBaseEntity
{
    public string Nom { get; set; }
    public string? Telephone { get; set; }
    public string? Email { get; set; }
    public string? Address { get; set; }
    public string? ImmatriculeFiscale { get; set; }
    public string? NumeroRegistreCommerce { get; set; }

    public IEnumerable<IPartenaireContact> PartenaireContacts { get; set; }
    public IEnumerable<PartenaireActivite> PartenaireActivite { get; set; }

    public virtual ICollection<PartenaireDetailsBancaire> PartenaireDetailsBancaires { get; set; }

}

在 Entites/Base 文件夹中

public interface IPartenaireContact : IAuditableEntity, ISoftDelete
{
    public Guid PartenaireId { get; set; }
    public Partenaire Partenaire { get; set; }
}

所以我的Partenaire可以有一个或多个PartenaireContact,PartenaireContact实际上继承自IdentityUser,因为PartenaireContact是可以登录应用程序的用户

在“基础设施”解决方案中:

数据/身份文件夹

public class PartenaireContact : IdentityUser, IPartenaireContact
{
    public Guid PartenaireId { get; set; }
    public Partenaire Partenaire { get; set; }

    public string Nom { get; set; }
    public string Prenom { get; set; }
    public string Identifiant { get; set; }
    public string? FullName { get; set; }
    public string? Fonction { get; set; }
    public string? Tel { get; set; }
    public string? Note { get; set; }
    public string? ImageUrl { get; set; }
    public bool IsActive { get; set; }
    public string? RefreshToken { get; set; }
    public DateTime RefreshTokenExpiryTime { get; set; }
    public Guid CreatedBy { get; set; }
    public DateTime Created { get; set; }
    public Guid LastModifiedBy { get; set; }
    public DateTime? LastModified { get; set; }
    public DateTime? DeletedOn { get; set; }
    public Guid? DeletedBy { get; set; }
}

在数据文件夹中

public class ApplicationDbContext : IdentityDbContext<PartenaireContact, IdentityRole, string>
{
    private readonly IDateTimeService _dateTime;
    private readonly ILoggerFactory _loggerFactory;
    private readonly ICurrentUser _currentUser;
    private readonly ISerializerService _serializer;
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
        IDateTimeService dateTime,
        ILoggerFactory loggerFactory,
        ICurrentUser currentUser,
        ISerializerService serializer) : base(options)
    {
        ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        _dateTime = dateTime;
        _loggerFactory = loggerFactory;
        _currentUser = currentUser;
        _serializer = serializer;
    }

    public DbSet<Partenaire> Partenaires { get; set; }
    public DbSet<Devise> Devises { get; set; }
    public DbSet<Activite> Activites { get; set; }
    public DbSet<PartenaireActivite> PartenaireActivites { get; set; }
    public DbSet<PartenaireDetailsBancaire> PartenaireDetailsBancaires { get; set; }
    public DbSet<Trail> AuditTrails => Set<Trail>();

    public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
    {
        var auditEntries = HandleAuditingBeforeSaveChanges();

        int result = await base.SaveChangesAsync(cancellationToken);

        await HandleAuditingAfterSaveChangesAsync(auditEntries, cancellationToken);

        return result;
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.AppendGlobalQueryFilter<ISoftDelete>(s => s.DeletedOn == null);
        
        builder.Entity<PartenaireActivite>().HasKey(pa => new { pa.Partenaire, pa.ActiviteId });
        
        builder.Entity<PartenaireDetailsBancaire>()
            .HasOne<Partenaire>(pdb => pdb.Partenaire)
            .WithMany(p => p.PartenaireDetailsBancaires)
            .HasForeignKey(pdb => pdb.PartenaireId);

        builder.Entity<PartenaireDetailsBancaire>()
            .HasOne<Devise>(pdb => pdb.Devise)
            .WithMany(d => d.PartenaireDetailsBancaires)
            .HasForeignKey(pdb => pdb.DeviseId);

        builder.Entity<IPartenaireContact>()
            .HasOne<Partenaire>(pdb => pdb.Partenaire)
            .WithMany(p => p.PartenaireContacts)
            .HasForeignKey(pdb => pdb.PartenaireId);

        base.OnModelCreating(builder);
        builder.ApplyConfigurationsFromAssembly(GetType().Assembly);
    }
}

所以当我尝试迁移时出现此错误:

指定类型 “ApplicationCore.Core.Entities.Base.IPartenaireContact”必须是 用作实体类型的非接口引用类型。

我知道我应该将

IPartenaireContact
作为我的关系中的类,但我不喜欢在我的“核心”解决方案中添加 EntityFramework。

请问有什么帮助吗?

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

我认为您应该使用 使用 EF Core 中 .Property 的通用版本来配置实体属性

你看到这个答案了吗?

© www.soinside.com 2019 - 2024. All rights reserved.