I have 2 models which have exactly same fields, but I chose to make different models for them because I needed two different tables, one for each.
Earlier everything was working fine when I had two different tables for each model, but then I started using abstract base class because the code inside both the models were same.
Now I have a single table comprised of all the data that I save.
How can I create different tables for those two models.
public abstract class baseGrammar
{
[Key]
public int Id { get; set; }
[Required]
public string question { get; set; }
[Required]
public string ans { get; set; }
public string ruleId { get; set; }
public string ruleApplicable { get; set; }
[ForeignKey("ruleId")]
public virtual ruleTable RuleTable { get; set; }
}
The one shown above is my abstract base class.
public class article : baseGrammar
{
}
public class adjective : baseGrammar
{
}
Just if someone intrested in ruleTable model.
public class ruleTable
{
[Key]
public string ruleId { get; set; }
public string topic { get; set; }
public string rule { get; set; }
public string example { get; set; }
public virtual ICollection<baseGrammar> BaseGrammar { get; set; }
}
Am also adding context class so as to provide better description
public class english : DbContext
{
public english() : base("name=localServerEng")
{
Database.SetInitializer<DbContext>(null);
Database.SetInitializer<english>(new UniDBInitializer<english>());
}
public virtual DbSet<adjective> adjectiveDb { get; set; }
public virtual DbSet<adverb> adverbDb { get; set; }
public virtual DbSet<alternativeVerb> alternativeVerbDb { get; set; }
public virtual DbSet<antonyms> antonymsDb { get; set; }
public virtual DbSet<article> articleDb { get; set; }
private class UniDBInitializer<T> : DropCreateDatabaseIfModelChanges<english>
{
}
public System.Data.Entity.DbSet<StructureSSC.Areas.AreaEnglish.Models.baseGrammar> baseGrammars { get; set; }
}
Screenshot of SQL Server showing 1 table comprising of all columns instead of different tables
This set up will give you 2 tables: (1) adjectives (2) articles
The context should be like this:
public class SomeContext : DbContext
{
public SomeContext()
: base("name=SomeContext")
{
}
public virtual DbSet<article> Articles { get; set; }
public virtual DbSet<adjective> Adjectives { get; set; }
}
public abstract class baseGrammar
{
//... common properties/columns
}
public class article : baseGrammar
{
}
public class adjective : baseGrammar
{
}
Please note the naming convention. In .NET class names and property names should follow Pascal Notation. Therefore, they should be:
BaseGrammar
Article
Adjective
RuleApplicable // other properties should follow same convention
Related
I'm having a trouble configuring May-to-many with TPC inheritance
public class TestB
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ParentClass> ParentClasss { get; set; }
}
public abstract class ParentClass
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TestB> TestBs { get; set; }
}
[Table("Child_A")]
public class Child_A: ParentClass
{
public string childAName { get; set; }
}
[Table("Child_B")]
public class Child_B: ParentClass
{
public string childbName { get; set; }
}
the many to many relation is on the abstract class, the generated tables are
TestB
ParentClass
ParentClassTestB : the many to many relation
Child_A : have FK for the ParentClass
Child_B : have FK for the ParentClass
what I need is to have the many to many directly with Child_A and Child_B.
so the generated tables will be something like
TestB
Child_A
Child_ATestB : the many to many relation table between Child_A and TestB
Child_B
Child_BTestB : the many to many relation table between Child_B and TestB
regards
what i need is to have the many to many directly with Child_A and Child_B.
Then don't map ParentClass as an Entity. You can still have it as the parent class in your code, but as far as the database is concerned Clild_A and Child_B are unrelated.
This is especially important in TPH, which has serious performance implications. This way there's simply no database overhead for your inheritance hierarchy. And the only real downside is that you don't have a built-in search over all ParentClass entities.
The only change is that if you don't want ParentClass involved in the M2M in the database, you can't have a navigation property from TestB to ParentClass. So
public class TestB
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Child_A> ParentClasssA { get; set; }
public ICollection<Child_B> ParentClasssB { get; set; }
}
public abstract class ParentClass
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TestB> TestBs { get; set; }
}
[Table("Child_A")]
public class Child_A : ParentClass
{
public string childAName { get; set; }
}
[Table("Child_B")]
public class Child_B : ParentClass
{
public string childbName { get; set; }
}
In EF Core 3.1.15 I manage a model with a generic. I would like to store the entities in the same table basis Table-Per-Hierarchy approach (TPH pattern). Below is the model abstracted. The resulting database creates 1 table for Part and descendants with a discriminator (as expected), but instead of 1 table for BaseComputer and descendants it creates a separate table for Computers and a separate table for Laptops (not expected).
namespace EFGetStarted
{
public class BloggingContext : DbContext
{
public DbSet<Computer> Computers { get; set; }
public DbSet<Laptop> Laptops { get; set; }
public DbSet<Part> Parts { get; set; }
}
public abstract class BaseComputer<T> where T : Part
{
[Key]
public int Id { get; set; }
public List<T> Parts { get; set; }
}
public class Computer : BaseComputer<Part>
{
public string ComputerSpecificProperty { get; set; }
}
public class Laptop : BaseComputer<LaptopPart>
{
public string LaptopSpecificProperty { get; set; }
}
public class Part
{
[Key]
public int Id { get; set; }
public string PartName { get; set; }
}
public class LaptopPart : Part
{
public string LaptopSpecificPartProperty { get; set; }
}
}
I tried explicitly specifying the entity as TPH:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BaseComputer<Part>>()
.HasDiscriminator()
.HasValue<Computer>("Computer")
.HasValue<Laptop>("Laptop");
}
But this fails with the following message:
The entity type 'Laptop' cannot inherit from 'BaseComputer' because 'Laptop' is not a descendant of 'BaseComputer'.
Questions: Is it possible for me to design this model in a TPH pattern? If not, is it because "Laptop is not a descendant of BaseComputer<Part>"? And if that's the case, why is not a considered a descendant and what should I change in the class to make it a descendant?
This is my first time here so sorry if I did not put the doubt by default. In my project I have two models class doubts and a class of works, I want to create a classification for both using a superclass, like the design pattern strategy. But I have a problem, the project state is advanced and I have used the doubt and works classes many times. Already tried in many ways, but either the error in the update-database or the error in what I had done before.
Does anyone know one way that I can implement a generic class classification for the doubts and works classes without many changes of what I had already done?
My code is below
public abstract class Classificable
{
[Key]
public int id { get; set; }
public virtual Classification classication { get; set; }
}
public class Doubt : Classificable
{
public int doubtID { get; set; }
public string question { get; set; }
public string content { get; set; }
public virtual Student student { get; set; }
public virtual Course course { get; set; }
public virtual Work work { get; set; }
public virtual ICollection<Answer> answers { get; set; }
}
public class Work : Classificable
{
public int workID { get; set; }
public string name { get; set; }
public string nameWork { get; set; }
public string filePath { get; set; }
public virtual Student student { get; set; }
public virtual Course course { get; set; }
public virtual ICollection<Doubt> doubts { get; set; }
}
public class DB_DIS : DbContext
{
public DB_DIS()
: base("name=DB_DIS")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Doubt>().ToTable("Doubts");
modelBuilder.Entity<Work>().ToTable("Works");
}
public virtual DbSet<Doubt> Doubts { get; set; }
public virtual DbSet<Work> Works { get; set; }
}`
What is the property or method in Classificable that you are trying to apply to Doubt and Work?
Have you written your data structure in such a way that is contains an id field AND workID feild?
In my experience, if you are trying to share properties between EF Classes you are better off sharing common fields. I've used it in the past for base classes of AuditableBase as follows:
public class AuditableBase
{
public string UpdateUserId { get; set; }
public DateTime UpdateDate { get; set; }
}
Then all my classes that I want to "Audit" will have a UpdateUserId and UpdateDate, and I can do some pre-save processing to set those based on the type of AuditableBase.
This is the content of my DivorceCases.cs file inside Models:
public class DivorceCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class DivorceCasesContext : DbContext
{
public DivorceCasesContext() : base("mssqlDB") { }
public DbSet<DivorceCases> DivorceCase { get; set; }
}
This is the content of my CorporationCases.cs file inside Models:
public class CorporationCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class CorporationCasesContext : DbContext
{
public CorporationCasesContext() : base("mssqlDB") { }
public DbSet<CorporationCases> CorporationCase { get; set; }
}
Now my question is:
I am using code-first approach to let EF6 create table for me automatically.
when I try to create and use any instance of table and EF6 creates it as well for DivorceCases Model and Context pair. But after the DivorceCases table has been created, I try to create and use CorporationCases Model instance then EF6 fails to automatically create the table for me because
"Transaction" Table has already been created by DivorceCases context
So how do I solve this issue?
You don't need to create 2 dbContext here.just use one as shown below.
Important Note : You're not following basic naming conventions.I highly recommend to use those.
one place is here : public DbSet<DivorceCases> DivorceCase { get; set; }
need to be corrected as public DbSet<DivorceCase> DivorceCases { get; set; }
Using One DbContext :
public class DivorceCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class CorporationCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class YourCasesContext : DbContext
{
public YourCasesContext () : base("mssqlDB") { }
public DbSet<DivorceCases> DivorceCase { get; set; }
public DbSet<CorporationCases> CorporationCase { get; set; }
}
Using code first, I have some abstract classes and some classes derived from those abstracted classes.
// Abstracted Classes
public abstract class Brand
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public abstract class Model
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
// Derived Classes
[Table("ComparisonBrand")]
public class ComparisonBrand : Brand
{
public ComparisonBrand()
{
ComparisonValues = new List<ComparisonValue>();
Models = new List<ComparisonModel>();
}
public virtual ICollection<ComparisonValue> ComparisonValues { get; set; }
public virtual ICollection<ComparisonModel> Models { get; set; }
}
[Table("ComparisonModel")]
public class ComparisonModel : Model
{
public int? BrandId { get; set; }
public int? LogoId { get; set; }
[ForeignKey("BrandId")]
public virtual ComparisonBrand ComparisonBrand { get; set; }
[ForeignKey("LogoId")]
public virtual ComparisonLogo ComparisonBrand { get; set; }
public virtual ICollection<ComparisonValue> ComparisonValues { get; set; }
}
My issue is that the migration generates foreign keys for:
ComparisonModel.Id > Models.Id
ComparisonModel.BrandId > Brands.Id
ComparisonModel.BrandId > ComparisonBrand.Id
Since ComparisonBrand.Id is a FK to Brands.BrandId, I get an error when deleting a Brand record. If I delete the ComparisonModel.BrandId > ComparisonBrand.Id relationship, however, the delete works fine.
How can I prevent a relationship from being formed between both the abstracted table and the derived table (Brands and ComparisonBrand)?
You are using the virtual keyword this causes Lazy Loading. You are telling EF to generate Foreign keys for them through this feature. Drop the virtual and you will not create the keys any longer