生成的实体框架代码

时间:2010-09-15 23:49:50

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

实体框架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(),当我查看引用时,不会在任何地方调用此方法。这种方法的目的是什么?

1 个答案:

答案 0 :(得分:4)

实体框架默认代码生成模板为模型中的每个实体对象创建Factory Method。此静态方法可让您快速创建新实体,其参数列表包含您班级中的所有非可空属性(而非所有属性)。
因此,这并不意味着保存或向DB提交任何内容。