'Prevent EF from creating table from subclasses
I have 2 classes; Product and Book. Book is subclass of Product. In my DbContext there is only DbSet of Product. I'm using EF Code-First approach. when EF creates migration class, properties of subclass 'Book' exist in migrationBuilder. I want only properties of base class 'Product' on my database. If you have any idea, help me.
Product.cs:
public class Product : BaseEfDomainModel, IFullDescription, IPicture<string>, ISeo
{
public string Tag { get; set; }
public string Json { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public double Mass { get; set; }
public Brand Brand { get; set; }
public double Width { get; set; }
public double Length { get; set; }
public double Height { get; set; }
public long? BrandId { get; set; }
public long SoldCount { get; set; }
public string Picture { get; set; }
public string Keywords { get; set; }
public long VisitCount { get; set; }
public string PictureAlt { get; set; }
public long FavoriteCount { get; set; }
public string Description { get; set; }
public string PictureTitle { get; set; }
public string MetaDescription { get; set; }
public ProductType ProductType { get; set; }
public string ShortDescription { get; set; }
public long? ProductClassLevel01Id { get; set; }
public long? ProductClassLevel02Id { get; set; }
public long? ProductClassLevel03Id { get; set; }
public long? ProductClassLevel04Id { get; set; }
public List<ProductPicture> ProductPictures { get; set; }
public ProductClassLevel01 ProductClassLevel01 { get; set; }
public ProductClassLevel02 ProductClassLevel02 { get; set; }
public ProductClassLevel03 ProductClassLevel03 { get; set; }
public ProductClassLevel04 ProductClassLevel04 { get; set; }
public string Dimensions { get => CalculateDimensions(); set { } }
public void Like() => FavoriteCount++;
public void Dislike() => FavoriteCount--;
public override void Edit<T>(T edited) => this.From(edited);
public string CalculateDimensions() => $"{Length} × {Width} × {Height}";
}
Book.cs:
public sealed class Book : Product
{
public string Seri { get; set; }
public string ISBN { get; set; }
public string Subject { get; set; }
public long PageCount { get; set; }
public string PrintYear { get; set; }
public string CoverType { get; set; }
public long? StudyFieldId { get; set; }
public long? StudyGradeId { get; set; }
public long? BookFormatId { get; set; }
public string AuthorsIdList { get; set; }
public List<Author> Authors { get; set; }
public StudyGrade StudyGrade { get; set; }
public BookFormat BookFormat { get; set; }
public StudyField StudyField { get; set; }
public sealed override void Edit<T>(T edited) => this.From(edited);
public List<long> AuthorIDs() => AuthorsIdList?.Split(IdSeparator).ToList().Select(long.Parse).ToList() ?? new();
}
migrationBuilder:
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Tag = table.Column<string>(type: "nvarchar(max)", nullable: true),
Json = table.Column<string>(type: "nvarchar(max)", nullable: true),
Name = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
Slug = table.Column<string>(type: "nvarchar(300)", maxLength: 300, nullable: false),
Mass = table.Column<double>(type: "float", nullable: false),
Width = table.Column<double>(type: "float", nullable: false),
Length = table.Column<double>(type: "float", nullable: false),
Height = table.Column<double>(type: "float", nullable: false),
BrandId = table.Column<long>(type: "bigint", nullable: true),
SoldCount = table.Column<long>(type: "bigint", nullable: false),
Picture = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
Keywords = table.Column<string>(type: "nvarchar(80)", maxLength: 80, nullable: false),
VisitCount = table.Column<long>(type: "bigint", nullable: false),
PictureAlt = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: true),
FavoriteCount = table.Column<long>(type: "bigint", nullable: false),
Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
PictureTitle = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
MetaDescription = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
ProductType = table.Column<int>(type: "int", nullable: false),
ShortDescription = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
ProductClassLevel01Id = table.Column<long>(type: "bigint", nullable: true),
ProductClassLevel02Id = table.Column<long>(type: "bigint", nullable: true),
ProductClassLevel03Id = table.Column<long>(type: "bigint", nullable: true),
ProductClassLevel04Id = table.Column<long>(type: "bigint", nullable: true),
Dimensions = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
Discriminator = table.Column<string>(type: "nvarchar(max)", nullable: false),
Seri = table.Column<string>(type: "nvarchar(max)", nullable: true),
ISBN = table.Column<string>(type: "nvarchar(max)", nullable: true),
Subject = table.Column<string>(type: "nvarchar(max)", nullable: true),
PageCount = table.Column<long>(type: "bigint", nullable: true),
PrintYear = table.Column<string>(type: "nvarchar(max)", nullable: true),
CoverType = table.Column<string>(type: "nvarchar(max)", nullable: true),
StudyFieldId = table.Column<long>(type: "bigint", nullable: true),
StudyGradeId = table.Column<long>(type: "bigint", nullable: true),
BookFormatId = table.Column<long>(type: "bigint", nullable: true),
AuthorsIdList = table.Column<string>(type: "nvarchar(max)", nullable: true),
BrandId1 = table.Column<long>(type: "bigint", nullable: true),
IsRemoved = table.Column<bool>(type: "bit", nullable: false),
CreationDate = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
table.ForeignKey(
name: "FK_Products_BookFormats_BookFormatId",
column: x => x.BookFormatId,
principalTable: "BookFormats",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_Brands_BrandId",
column: x => x.BrandId,
principalTable: "Brands",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_Brands_BrandId1",
column: x => x.BrandId1,
principalTable: "Brands",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_ProductClassesLevel01_ProductClassLevel01Id",
column: x => x.ProductClassLevel01Id,
principalTable: "ProductClassesLevel01",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_ProductClassesLevel02_ProductClassLevel02Id",
column: x => x.ProductClassLevel02Id,
principalTable: "ProductClassesLevel02",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_ProductClassesLevel03_ProductClassLevel03Id",
column: x => x.ProductClassLevel03Id,
principalTable: "ProductClassesLevel03",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_ProductClassesLevel04_ProductClassLevel03Id",
column: x => x.ProductClassLevel03Id,
principalTable: "ProductClassesLevel04",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_StudyFields_StudyFieldId",
column: x => x.StudyFieldId,
principalTable: "StudyFields",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Products_StudyGrades_StudyGradeId",
column: x => x.StudyGradeId,
principalTable: "StudyGrades",
principalColumn: "Id");
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|