实体框架2.1映射问题

时间:2018-09-17 22:41:58

标签: entity-framework asp.net-core-mvc nopcommerce

我将插件从.NET Framework 4.6.1升级到了.NET Core 2.1,并且EF映射出现问题。

我在4.6.1中有扩展基础图片类的类:

public partial class PictureExt : Picture
{
    public virtual string ExternalUrl { get; set; }
}

基类:

public partial class Picture : BaseEntity
{
    public string MimeType { get; set; }
    public string SeoFilename { get; set; }
    public string AltAttribute { get; set; }
    public string TitleAttribute { get; set; }
    public bool IsNew { get; set; }
    public virtual PictureBinary PictureBinary { get; set; }
}

在4.6.1中,我完成了映射:

    public PictureMap()
    {
        this.ToTable("Picture");
        this.HasKey(p => p.Id);
        this.Property(p => p.PictureBinary).IsMaxLength();
        this.Property(p => p.MimeType).IsRequired().HasMaxLength(40);
        this.Property(p => p.SeoFilename).HasMaxLength(300);
        this.Property(p => p.ExternalUrl).IsOptional();
    }

一切都很好

但是在.NET Core EF中,我完成了映射:

    public override void Configure(EntityTypeBuilder<PictureExt> builder)
    {
        builder.ToTable("Picture");
        builder.HasKey(picture => picture.Id);
        builder.Property(picture => 
        picture.MimeType).HasMaxLength(40).IsRequired();
        builder.Property(picture => picture.SeoFilename).HasMaxLength(300);
        builder.Property(p => p.ExternalUrl);
        base.Configure(builder);
    }

我有一个例外:

A key cannot be configured on 'PictureExt' because it is a derived type. The key must be configured on the root type 'Picture'. If you did not intend for 'Picture' to be included in the model, ensure that it is not included in a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation property on a type that is included in the model

我了解我做错了什么,但我不知道该怎么做才对。 也许是这样的吗? https://stackify.com/new-in-net-core-2-1/#post-19576-_kaymrlea07yf

但是如何实现呢?

2 个答案:

答案 0 :(得分:0)

您可以做的是确保您的DbSet<Picture>上没有DbSet<BaseEntity>DbContext。如果以下解决方案无法解决您的问题,请更新您的问题,以便我们相应地更新我们的答案。 :)

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using Xunit;

public class Tests
{
    [Fact]
    public void Can_add_PictureExt()
    {
        var options = new DbContextOptionsBuilder<Context>()
            .UseInMemoryDatabase(Guid.NewGuid().ToString())
            .Options;

        using (var ctx = new Context(options))
        {
            ctx.Add(new PictureExt());
            ctx.SaveChanges();
        }

        using (var ctx = new Context(options))
        {
            Assert.Single(ctx.PictureExt);
        }
    }
}
public class Context : DbContext
{
    public Context(DbContextOptions<Context> options) : base(options) { }

    public DbSet<PictureExt> PictureExt { get; set; }

    //uncommenting any of the following DbSet will throw exception

    //public DbSet<Picture> Picture { get; set; }

    //public DbSet<BaseEntity> BaseEntity { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new PictureExtConfiguration());
    }
}
public class PictureExtConfiguration : IEntityTypeConfiguration<PictureExt>
{
    public void Configure(EntityTypeBuilder<PictureExt> builder)
    {
        builder.ToTable("Picture");
        builder.HasKey(picture => picture.Id);
        builder.Property(picture =>
        picture.MimeType).HasMaxLength(40).IsRequired();
        builder.Property(picture => picture.SeoFilename).HasMaxLength(300);
        builder.Property(p => p.ExternalUrl);

        //commented out to simplify example
        //base.Configure(builder);
    }
}
public partial class PictureExt : Picture
{
    public virtual string ExternalUrl { get; set; }
}
public partial class Picture : BaseEntity
{
    public string MimeType { get; set; }
    public string SeoFilename { get; set; }
    public string AltAttribute { get; set; }
    public string TitleAttribute { get; set; }
    public bool IsNew { get; set; }

    //commented out to simplify example
    //public virtual PictureBinary PictureBinary { get; set; }
}
public class BaseEntity
{
    public int Id { get; set; }
}

答案 1 :(得分:0)

单行修复:

public override void Configure(EntityTypeBuilder<PictureExt> builder)
{
    builder.ToTable("Picture");
    
    // turn off deriving
    builder.HasBaseType((Type) null);

    builder.HasKey(picture => picture.Id);
    builder.Property(picture => 
    picture.MimeType).HasMaxLength(40).IsRequired();
    builder.Property(picture => picture.SeoFilename).HasMaxLength(300);
    builder.Property(p => p.ExternalUrl);
    base.Configure(builder);
}