【问题标题】:Creating different tables using using same base abstract class使用相同的基本抽象类创建不同的表
【发布时间】:2017-12-31 21:37:30
【问题描述】:

我有 2 个具有完全相同字段的模型,但我选择为它们制作不同的模型,因为我需要两个不同的表,每个表一个。

早些时候,当我为每个模型有两个不同的表时,一切正常,但后来我开始使用抽象基类,因为两个模型中的代码是相同的。

现在我有一个包含我保存的所有数据的表。

如何为这两个模型创建不同的表。

 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; }
}

上面显示的是我的抽象基类。

public class article : baseGrammar
{

}
 public class adjective : baseGrammar
{
}

如果有人对 ruleTable 模型感兴趣。

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; }
}

我还添加了上下文类以便提供更好的描述

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

【问题讨论】:

  • How can i create different tables for those 2 models. 为每个创建表?目前尚不清楚您为什么不能这样做。
  • 您的上下文是否有 2 个DbSet 属性,一个用于article,一个用于adjective,还是只有baseGrammar
  • 它有 2 个 dbset 属性,一个用于文章,另一个用于形容词 @DavidG
  • 当使用基类时,只会创建一个包含两个模型@Will 的数据的表
  • OOoooh,我想你想要 Table per Type 继承策略 -- entityframeworktutorial.net/code-first/…

标签: c# asp.net-mvc database-design ef-code-first


【解决方案1】:

此设置将为您提供 2 个表格:(1) 形容词 (2) 文章

上下文应该是这样的:

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
{
}

请注意命名约定。在 .NET 中,类名和属性名应遵循 Pascal 表示法。因此,它们应该是:

BaseGrammar
Article
Adjective
RuleApplicable // other properties should follow same convention

【讨论】:

  • 我在上下文文件中添加了虚拟关键字,除了所有代码都相同。我现在在问题中添加了更多信息。问题仍未解决。 @CodingYoshi
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
相关资源
最近更新 更多