简体   繁体   English

code -first实体框架 - 继承和导航属性是否可行?

[英]code -first Entity framework - Is this possible with inheritance and navigation properties?

I haven't been able to figure this one out. 我无法弄明白这一点。

public abstract class A
{
    public int property1 { get; set; }
    public int property2 {get; set; }
}

public class B : A
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public IList<C> C { get; set; }

}

public class C : A
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AId { get; set; }

    public int dummyproperty {get; set; }

    public int BId { get; set; }

    [ForeignKey("BId")]
    public virtual B B { get; set; }
}

How can I have my database context build these 2 tables using these class B and C? 如何让我的数据库上下文使用这些B和C类构建这两个表?

class Program
    {
        static void Main(string[] args)
        {
            var context = new MyContext();
            foreach(var b in context.Bs)
            {
                Console.WriteLine(b.ToString());
            }
        }
    }

    public class MyContext : DbContext
    {
        public DbSet<C> Cs { get; set; }
        public DbSet<B> Bs { get; set; }
    }

And then in Package Manager Console, run commands: enable-migrations add-migration Initial update-database 然后在Package Manager Console中,运行命令:enable-migrations add-migration初始更新数据库

And then add some records for B and C in file 'Configuration.cs' method Seed. 然后在文件'Configuration.cs'方法Seed中添加B和C的一些记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架 5(代码优先)导航属性 - Entity Framework 5 (Code First) Navigation Properties 实体框架6-基类的继承和导航属性 - Entity Framework 6 - inheritance and navigation properties on base class 在视图(实体框架6 + MVC 5)中设置查询,导航属性(“代码优先”) - Setup query, navigation properties (Code First) in view (Entity Framework 6 + MVC 5) 实体框架代码首先使用导航属性一键 - Entity framework code first using navigation properties a Key .NET Entity Framework 代码优先:未加载导航属性 - .NET Entity Framework code-first: navigation properties are not loaded 在创建时分配实体框架代码优先导航属性 - Assigning Entity Framework Code First Navigation Properties on Create 实体框架-代码优先/ Fluent API-过滤的导航属性 - Entity Framework - Code First / Fluent API - Filtered navigation properties 是否可以在实体框架中删除链接的导航属性 - Is it possible remove chained navigation properties in Entity Framework 实体框架6:使用界面作为导航属性可能吗? - Entity Framework 6: Using interface as navigation properties possible? 实体框架代码优先,与继承的1:1关联 - Entity Framework Code First, 1:1 association with inheritance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM