Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Entity Framework Core and Inheritance

$
0
0

I've got a more complex EF Core problem.  I have a class Card which is the base class (objects can exist of this type) and class SpecialCard (objects can also exist of this type as defined below. At the moment SpecialCard only has a couple of things in it but could have more:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace TCGCollector.Models
{
    public class Card
    {
        public int CardID { get; set; }
        public string CardName { get; set; }
        public string CardImageURL { get; set; }
        public string CardImageHiURL { get; set; }

        //Foreign Key for Card Category
        public Nullable<int> CardCatID { get; set; }
        public CardCat CardCat { get; set; }

        //Foreign Key for Card Type
        public Nullable<int> CardTypeID { get; set; }
        public CardType CardType { get; set; }

        //Foreign Key for Set
        public Nullable<int> SetID { get; set; }
        public Set Set { get; set; }

        public int CardNum { get; set; }
        public string Artist { get; set; }

        //Foreign Key for Card Rarity
        public Nullable<int> CardRarityID { get; set; }
        public CardRarity CardRarity { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime LastUpdateDate { get; set; }
    }
}

and

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace TCGCollector.Models
{
    public class SpecialCard : Card
    {
        public string SpecialTypeName { get; set; }
        [StringLength(1024)]
        public string SpecialCardText { get; set; }
    }
}

However, I've just been looking at the JSON that this is going to be loaded and the SpecialCardText is actually an array of items, i.e. it's text per line of text not one big string as I first thought

    {"id": "1","name": "Name","imageUrl": "","subtype": "SubType","supertype": "Super","number": "1","artist": "","rarity": "Rarity","series": "Series","set": "Set","setCode": "Code","text": ["Text Line 1","Text Line 2"
        ],"imageUrlHiRes": ""
    }

Where the text elements are the ones which go into the SpecialText field.

Can someone explain how I would represent this in a code first way.  I'm presuming I'm going to need to create a many-to-many relationship like I've done here:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace TCGCollector.Models
{
    public class CardCollection
    {
        public int CardCollectionID { get; set; }
        public string CardCollectionName { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime CreatedDate { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime LastUpdateDate { get; set; }
        public ICollection<UserCardCollection> UserCardCollections { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace TCGCollector.Models
{
    public class User
    {
        public int UserID { get; set; }
        public string UserLogin { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime CreateDate { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime LastUpdateDate { get; set; }
        public ICollection<UserCardCollection> UserCardCollections { get; set; }
    }
}

using System;
namespace TCGCollector.Models
{
    public class UserCardCollection
    {
        public int UserID { get; set; }
        public User User { get; set; }
        public int CardCollectionID { get; set; }
        public CardCollection CardCollection { get; set; }
    }
}

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.DependencyInjection;

namespace TCGCollector.Models
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserCardCollection>()
                .HasKey(ucc => new { ucc.UserID, ucc.CardCollectionID });
            modelBuilder.Entity<UserCardCollection>()
                .HasOne(ucc => ucc.User)
                .WithMany(u => u.UserCardCollections)
                .HasForeignKey(ucc => ucc.UserID);
            modelBuilder.Entity<UserCardCollection>()
                .HasOne(ucc => ucc.CardCollection)
                .WithMany(cc => cc.UserCardCollections)
                .HasForeignKey(ucc => ucc.CardCollectionID);
        }

        public DbSet<CardCat> CardCats { get; set; }
        public DbSet<CardType> CardTypes { get; set; }
        public DbSet<CardRarity> CardRarities { get; set; }
        public DbSet<SetSeries> SetSeries { get; set; }
        public DbSet<Set> Sets { get; set; }
        public DbSet<PokemonType> PokemonTypes { get; set; }
        public DbSet<Card> Cards { get; set; }
        public DbSet<SpecialCard> SpecialCards { get; set; }
        public DbSet<TrainerCard> TrainerCards { get; set; }
        public DbSet<User> Users { get; set; }
    }
}

I've no idea how this would be structured coupled with the inherited class though.

Rob


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>