实体框架2.1映射的问题

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

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

我在4.6.1中有类扩展基类Picture类:

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

但是如何实施呢?

entity-framework asp.net-core-mvc nopcommerce
1个回答
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; }
}
© www.soinside.com 2019 - 2024. All rights reserved.