c# - 生成的 Entity Framework 代码

标签 c# .net asp.net-mvc entity-framework entity-framework-4

Entity Framework 4 生成的代码——主要是 AddTo 和 Create 方法——我应该直接使用它们吗?我试图了解 Create 方法。例如,CreateDinner 方法(在 NerdDinner 中)是这样的:

public static Dinner CreateDinner(global::System.Int32 dinnerID, global::System.String title, global::System.DateTime eventDate, global::System.String description, global::System.String hostedBy, global::System.String contactPhone, global::System.String address, global::System.String country, global::System.Double latitude, global::System.Double longitude)
    {
        Dinner dinner = new Dinner();
        dinner.DinnerID = dinnerID;
        dinner.Title = title;
        dinner.EventDate = eventDate;
        dinner.Description = description;
        dinner.HostedBy = hostedBy;
        dinner.ContactPhone = contactPhone;
        dinner.Address = address;
        dinner.Country = country;
        dinner.Latitude = latitude;
        dinner.Longitude = longitude;
        return dinner;
    }

没有 SubmitChanges(),当我查看引用时,没有在任何地方调用此方法。这个方法的目的是什么?

最佳答案

Entity Framework 默认代码生成模板创建一个 Factory Method对于模型中的每个实体对象。此静态 方法可让您快速创建一个新实体,它的参数列表包含类中所有不可为空的属性(而不是全部)。
因此,它并不意味着将任何内容保存或提交到数据库。

关于c# - 生成的 Entity Framework 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3722712/

相关文章:

c# - 程序集绑定(bind) - 使用了错误版本的 Unity,但为什么呢?

c# - Autofac + Automapper + 自定义 ValueResolver

javascript - 为什么对同一 ASP.NET MVC 操作的多个同时 AJAX 调用会导致浏览器阻塞?

javascript - ASP.NET:如何更新 JQuery 或 Javascript 中的隐藏字段?

c# - 插入/更新多对多 Entity Framework 。我该怎么做?

c# - 从文件夹中的所有文件创建 zip 文件

c# - 通过公共(public)属性访问私有(private)变量

javascript - Toggle 不使用新浏览器

c# - 使用 Visual Studio 和 NUnit 连接到 localdb

c# - 正则表达式只允许 0-9 和 - 字符,以及点 (.)