多重继承(也许是抽象类?)C#实体框架 [英] Multiple Inheritance (maybe Abstract class?) C# Entity Framework

查看:57
本文介绍了多重继承(也许是抽象类?)C#实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几周,我一直在使用Visual Studio 2015在c#上基于Entity Framework 6(代码优先)开发数据库。

The last weeks I have been working on the development of a database based on Entity Framework 6 (Code-First) on c# using Visual Studio 2015.

I' m目前正在处理继承提供的所有选项。
此时,按照数据库的标准,我应该使用主键实现多重继承。
这是什么意思?
这意味着我应该实现一个从另一个类继承的类,并具有自己的PK来标识他与父类不同。在这一点上,我可以用TPC来实现它,使父类成为抽象类,而不在父类上定义任何PK。这是此时的代码示例(可以正常工作,并且我已经对其进行了测试)

I'm currently working on all the options that inheritance offers to work with. At this point and following the standards of a database I should implement multiple inheritance with Primary Key. What this means? This means that I should implement a class that inherits from another class and have his own PK for identify him different from the parent class. At this point I could implement it with TPC, making the parent class abstract and not defining any PK on the parent. This is a sample of the code at this point (this works and I have tested it)

    public abstract class Person
    {

        public string Name { get; set; }
        public string LastName { get; set; }

    }

    [Table("Students")]
    public class Student : Person
    {

        [Key]public int Id_student { get; set; }
        public string code_s { get; set; }
        public virtual ICollection<Course> courses { get; set; }

    }

然后建立数据库应遵循的标准我要实现另一个继承,将Student作为父级,并创建另一个继承自Student的类。
我第一个愚蠢的想法是使编写变得简单

Then the standard I should follow to build my database requires me to implement another inheritance taking Student as the Parent and creating another class that inherits from Student. The first and stupid idea I had was to make it as simple as write

ChildOfChild : Student

但是显然没有用。

然后我想到了使学生类为Abstract的可能性,但是当我声明学生类Abstract时,没有让我实例化它并为表植入种子。我不确定是否可以使用抽象类或任何其他对开发此方法有用的方法来执行此操作。
如果您不了解我要解决的问题,这是我希望拥有并可以正常工作的代码示例。

Then it come to my mind the possibility of make Student class Abstract, but when I declared the Student class Abstract it didn't let me instantiate it and seed the table. I'm not sure if there is any other way to do this, using abstract class or any other method that could be useful to develop this. If you didn't understand the problem I'm trying to solve this is the sample of code that I would like to have and works.

    public abstract class Person
    {

        public string Name { get; set; }
        public string LastName { get; set; }

    }

    [Table("Students")]
    public class Student : Person
    {

        [Key]public int Id_student { get; set; }
        public string code_s { get; set; }
        public virtual ICollection<Course> courses { get; set; }

    }

    [Table("ExchangeStudent")]
    public class ExchangeStudent : Student
    {

        [Key]public int Id_exchange { get; set; }
        public string HomeUniversity {get; set;}

    }


推荐答案

据我了解,您希望两个表都具有各自的主键和一个外键来定义一对一的关系。在您的C#代码中,您希望这些表具有继承关系。

From my understanding you want two tables where both with their own primary keys and a foreign key to define a one to one relationship. In your C# code you want these tables to have an inheritance relationship.

我看到您使用Entity Framework进行此操作的唯一方法是拆分您的需求。首先创建代表所需表结构的数据库模型。

The only way I can see you doing this with Entity Framework is to split your requirements. Firstly create the Database models that represent the table structure you want.

namespace DAO
{
    public abstract class Person
    {
        public string Name { get; set; }
    }

    public class Student
    {
        public int StudentId { get; set; }
        public string Nickname { get; set; }
    }

    //Use navigation property rather than inheritance to define the relationship
    public class ExchangeStudent 
    {
        public int ExchangeStudentId { get; set; }
        public string HomeUniversity { get; set; }
        public virtual Student Student { get; set; }
    }
}

这给了我以下代码:

 public override void Up()
        {
            CreateTable(
                "dbo.ExchangeStudent",
                c => new
                    {
                        ExchangeStudentId = c.Int(nullable: false, identity: true),
                        HomeUniversity = c.String(),
                        Student_StudentId = c.Int(),
                    })
                .PrimaryKey(t => t.ExchangeStudentId)
                .ForeignKey("dbo.Student", t => t.Student_StudentId)
                .Index(t => t.Student_StudentId);

            CreateTable(
                "dbo.Student",
                c => new
                    {
                        StudentId = c.Int(nullable: false, identity: true),
                        Nickname = c.String(),
                        Name = c.String(),
                    })
                .PrimaryKey(t => t.StudentId);

        }

现在,如果您需要在Student和ExchangeStudent,您可以创建新对象以在不同的命名空间中定义

Now if you need to have a inheritance relationship between Student and ExchangeStudent you can create new objects to define in a different namespace

namespace BusinessObject
{
    public abstract class Person
    {
        public string Name { get; set; }
    }

    public class Student
    {
        public int StudentId { get; set; }
        public string Nickname { get; set; }
    }

    //ExchangeStudent derives from Student
    public class ExchangeStudent : Student
    {
        public int ExchangeStudentId { get; set; }
        public string HomeUniversity { get; set; }
    }
}

然后,您只需创建一个可映射的类这两个对象。也有第三方库可以为您完成此操作。

Then you just need to create a class that maps the two objects. There are 3rd party libraries that can do this for you as well.

这篇关于多重继承(也许是抽象类?)C#实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆