简体   繁体   English

实体框架不会从数据库生成所有表

[英]Entity Framework doesn't generate all tables from database

This is my diagram in my database: 这是我数据库中的图表:

在此处输入图片说明

but when I use Entity Framework it was like that: 但是当我使用实体框架时,就像这样:

在此处输入图片说明

It hasn't table name ListSuiteQuestion but It has 2 new property in class Question and Suite : 它没有表名ListSuiteQuestion但在QuestionSuite类中有2个新属性:

enter image description here 在此处输入图片说明

Table ListSuiteQuestion is automatically created by sql because in sql we don't have something called many to many relationship (m:n) and sql creates another table to implement m:n relationship with keys containing the primary key of two relationship tables is also the name of the combination of the names of the two relation tables. ListSuiteQuestion由sql自动创建,因为在sql我们没有所谓的many to many relationship (m:n)而sql创建了另一个表以实现m:n关系,其中键包含两个关系表的主键也是两个关系表的名称组合的名称。 Within your entity framework by accessing each table through another table you have access to that table so there is no need to define it by entity framework. 在您的实体框架内,通过另一个表访问每个表,您就可以访问该表,因此无需通过实体框架对其进行定义。 However, if you intend to customize or add a field to a third table you can manually build it into the code and then display it entity framework, though you don't need to. 但是,如果您打算自定义字段或将字段添加到第三个表,则可以将其手动构建到代码中,然后显示它的实体框架,尽管这不是必需的。

if you want create it manually in code do like this : 如果要在代码中手动创建它,请执行以下操作:

 public class Suite
    {
        //another property
        public int IdSuite { get; set; }
        public virtual ICollection<SuitQuestions> Questions { get; set; }
    }
    public class Question
    {
        //another property
        public int IdQuestion { get; set; }
        public virtual ICollection<SuitQuestions> Suites { get; set; }
    }
    public class SuiteQuestions
    {
        public Suite Suite { get; set; }
        public int IdSuite { get; set; }
        public  Question Question { get; set; }
        public int IdQuestion { get; set; }

        //add custome property if you need

    }

and config it. 并配置它。

It's correct. 这是正确的。 A question has a list of suites and a suite has a list of questions. 一个问题有一个套房列表,而一个套房有一个问题列表。 If you do: 如果您这样做:

        var suite = context.Suites.Find(5);
        var question = context.Questions.Find(30);
        suite.Questions.Add(question);
        // And update this suite object here;

You will see a new record in ListSuiteQuestion Tables with IdSuite = 5 and IdQuestion = 30. The class ListSuiteQuestion doesn't need to be created. 您将在ListSuiteQuestion表中看到一个新记录,其中IdSuite = 5且IdQuestion =30。不需要创建类ListSuiteQuestion。 However, if you really want to create the class you have to add Id to the Table ListSuiteQuestion as primary key. 但是,如果您确实要创建类,则必须将ID作为主键添加到Table ListSuiteQuestion中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM