【问题标题】:Entity framework AutoGenerated Entity实体框架自动生成的实体
【发布时间】:2018-04-03 08:39:37
【问题描述】:

我指的是实体框架教程,我从中了解到,当我们将数据库访问到框架中时,它会自动为我们要使用的所有表创建实体。
所以在这里我的问题是我可以使用我自己创建的表类文件而不是使用自动生成的实体吗?这是一种好习惯,还是我只需要使用这些实体作为一种好习惯。
请告诉我哪个应该是一个好方法。
例如。如果我有一个数据库StudentInfo,其中有Student 表,那么如果我使用EF 将它访问到我的项目中。默认情况下,它将为学生创建实体。但是如果我再次为 Student 创建自己的类文件,这将是一个好方法或需要使用默认生成的实体。

【问题讨论】:

标签: c# asp.net entity-framework-6


【解决方案1】:

我了解您使用的是数据库优先方法,因此您将表导入到您的项目中,Visual Studio 正在生成与您的实体、DBContext 等相关的代码...在这种情况下,您必须 使用那些实体类作为 DBContext 引用它们。

您可以对生成的代码进行一些控制,因为您可以更改类/属性名称、类型等...您还可以扩展这些生成的类,因为它们是部分类。

无论如何,如果您想使用自己的类,您应该遵循Code First 方法。这将需要更多的工作来定义与 DB 模型的所有映射,但您肯定可以使用自己的类。

【讨论】:

  • 好的。但假设在我的学生表中我有 10 列,我只想查询特定列,那么我仍然需要获取查询中的所有列。否则,如果我创建了自己的类文件,我应该只提到我想要从学生表中获得的那些列
  • 如果您根本不想使用这些列,可以在映射中删除它们。如果你想定义某种简化的实体,那么我们正在谈论一个不同的主题,上面提到的 ViewModel 或 DTO。您可能希望保留生成的完整实体类并将项目/映射到该视图模型类中。
  • 好的。非常感谢您宝贵的时间。最好使用实体类而不是自己创建的
  • 好吧,可能关键在于他们映射数据库的方式是唯一的选择,这并不意味着为实体使用您自己的类是一种不好的做法。看看 EF Code First 方法,每种方法都有其优点和缺点。我很确定 SO 中有大量关于此的讨论,您会发现支持每个人的意见。
  • 我有一个现有的数据库,我在我的项目中使用 EF 访问它,但为了查询,我仍然使用我自己创建的模型类文件,这就是为什么我感到困惑的是正确与否。因为我正在从我的表中创建 API 服务。
【解决方案2】:

如果您创建自己的类而不是 EF 生成的类,则必须定义某种映射,框架通过该映射了解您的自定义类代表 DB 实体本身。

因此,使用 EF 生成的实体并在这些实体上创建您自己的自定义视图模型以在您的应用程序中使用是有意义的。

【讨论】:

  • 如果我使用 DTO 而不是使用 Viewmodels 呢?仅获取该 DTO 类文件中的选定列
  • DTO 太有意义了,它只会包含您确切需要的那些列或属性。您可以查询您的数据库上下文以返回您所需的列,然后可以将这些列映射到您的 DTO 属性。
【解决方案3】:

好吧,你想告诉我的是,你想使用自己生成的类。这是一个很好的做法,你应该尝试让每一件事都成为你自己的。实体农场工作的主要问题是,当它为一个表创建任何类,它将它放在项目的主目录中,并命名为我认为 EDMX,但你仍然需要做很多工作。而是使用实体框架来创建类。如果要更新表的特定列,只需使用特定列名运行更新查询。 在这里,我创建了一个函数,它将为您的所有表分类,只需更改代码中的连接字符串并享受........此函数将返回一个字符串,只需将其复制并传递给您的上课和享受。总是尝试制作自己的......使用这种类型的编程,即使你也可以制作页面............是的

          public string makeclassandfunctions(string tablename)
    {
        DataSet ds = loadtabledata(tablename);
        //the class starts frome here
        string classoftable = "</br></br></br>public class   " + tablename + "</br>{   " + "</br>";

        for (int a = 0; a <= ds.Tables[0].Columns.Count-1 ; a++)
        {
            if (ds.Tables[0].Columns[a].ColumnName.Contains("ID"))
            {
                classoftable += "public Int32 " + ds.Tables[0].Columns[a].ColumnName + "{get;set;}" + "</br>";
            }
            else
            {
                classoftable += "public string   " + ds.Tables[0].Columns[a].ColumnName + "{get;set;}" + "</br>";
            }

        }
        //class is ending here its variables are defined here

        classoftable += "} ";
        //the insertion function starts frome here 
        classoftable += "</br>public void insertinto" + tablename + "(" + tablename + "  obj" + tablename + ")</br>{ </br> ";
        classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" insert into " + tablename + " values " + "(";
        for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
        {
            if (a == ds.Tables[0].Columns.Count - 1)
            {
                classoftable += "@" + ds.Tables[0].Columns[a].ColumnName;
            }
            else
            {
                classoftable += "@" + ds.Tables[0].Columns[a].ColumnName + " , ";
            }

        }
        classoftable += ")" + @""",sqlcon);</br>";

        for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
        {
            classoftable += "cmd.Parameters.AddWithValue (" + @"""@" + ds.Tables[0].Columns[a].ColumnName + @""" ,obj" + tablename + "." + ds.Tables[0].Columns[a].ColumnName + ");</br>";
        }
        classoftable += " cmd.ExecuteNonQuery();</br>sqlcon.Close();";
        classoftable += "}</br></br>";
        //insert function ends here

        //delete function startsfrome here

        classoftable += "public void deletefrom" + tablename + "(" + "Int32 id" + ")" + "</br>{ </br> ";

        classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" delete  from [" + tablename + "]" + "  where " + ds.Tables[0].Columns[0].ColumnName + "= " + @"'" + @"""" + "+id+" + @"""" + @"'" + @"""" + ",sqlcon);" + "</br> cmd.ExecuteNonQuery();</br>sqlcon.Close();</br>}</br></br>";


        //delete ends here

        //updatefunctionstarts frome here
        classoftable += "public void update" + tablename + "(" + tablename + "  obj" + tablename + " )</br>{";

        classoftable += " sqlcon.Open();</br> SqlCommand cmd = new SqlCommand(" + @""" update  [" + tablename + "] set  ";

        for (int a = 1; a <= ds.Tables[0].Columns.Count - 1; a++)
        {
            string columnname = ds.Tables[0].Columns[a].ColumnName;

            if (a == ds.Tables[0].Columns.Count - 1)
            {
                classoftable += columnname + " = @" + columnname + "  ";

                classoftable += "where  " + ds.Tables[0].Columns[0].ColumnName + " = @" + ds.Tables[0].Columns[0].ColumnName;
            }
            else
            {
                classoftable += columnname + " = @" + columnname + " , ";
            }
        }
        classoftable += @"""" + ",sqlcon);</br>";

        for (int a = 0; a <= ds.Tables[0].Columns.Count - 1; a++)
        {
            classoftable += "cmd.Parameters.AddWithValue (" + @"""@" + ds.Tables[0].Columns[a].ColumnName + @""" ,obj" + tablename + "." + ds.Tables[0].Columns[a].ColumnName + ");</br>";
        }
        classoftable += "cmd.ExecuteNonQuery();</br>sqlcon.Close();</br>}</br>";


        //updatesstatementfunction ends here





        return classoftable;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多