2011-11-05 56 views
1

我有一个使用数据库优先方法生成的EF模型。在设计器上,我使用DBContext对象创建了一个“代码生成项目”。这创建了一个模板,为我的表生成POCO类(我想要的)。EntityFramework生成的类

我一直在使用这些相同的类在它们的WCF和DataAnnotation属性中工作正常。我保存了这个文件的一个副本,如果模型重新生成,我只需将旧代码粘贴到新创建的模型生成的类中,并更新所有新属性。

我试着走一步。每当我改变模型时,类就会重新生成,属性也会丢失。我试图做的是在我的项目中创建一个单独的文件夹,其中的名称空间符合它的类名。我基本上会将生成的POCO类中已更改的任何属性复制到我创建的新文件夹中的其他类中,并简单地添加所需的任何属性。但是,这与上面第二段的内容几乎完全相同。但是,如果有大型数据库模型,这可能会变得乏味且容易出错。

我想要做的是以某种方式模拟生成模型而不会丢失属性。是的,我知道---吃我的蛋糕,也吃它...

任何想法?

我试着添加好友类,但那不起作用。

数据遇到了问题,但在添加好友类后,我在下面做了DataAnnotations不起作用。

我想,也许我需要改变我的服务的方法包括:Customer_Validation对象,而不是客户,做客户端的相同。

我将要做出这种改变,但遇到了对我的服务方法,下面的代码片段路障(这是即使我改变了的DbContext当然这是另一个代码生成的类的definiton。) 。 p.CustomerID上有设计时编译错误。它不存在。

IQueryable<**Customer_Validation**> customer = DbContext.Customers.Where(p => **p.CustomerID** > 0); 


public DbSet<**Customer_Validation**> Customers { get; set; } 

我是什么,以获得DataAnnotations工作缺少什么?你的帮助是非常感谢:)

我有我的客户类以下。

using System; 
using System.Collections.Generic; 
using System.ServiceModel; 
using System.Runtime.Serialization; 
using System.ComponentModel.DataAnnotations; 
using DataAnnotationsExtensions; 

namespace YeagerTechModel 
{ 
    [MetadataType(typeof(Customer_Validation))] 
    public partial class Customer 
    { 

    } 

    public partial class Customer_Validation 
    { 
     [Serializable] 
     [DataContract(IsReference = true)] 
     public class Customer1 
     { 
      public Customer1() 
      { 
       this.Projects = new HashSet<Project>(); 
      } 

      [DataMember] 
      public short CustomerID { get; set; } 

      [DataMember] 
      [Required] 
      [StringLength(50)] 
      [DataType(DataType.EmailAddress)] 
      [Email] 
      public string Email { get; set; } 

      [DataMember] 
      [StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")] 
      [DataType(DataType.Text)] 
      public string Company { get; set; } 

      [DataMember] 
      [StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")] 
      [DataType(DataType.Text)] 
      public string FirstName { get; set; } 

      [DataMember] 
      [StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")] 
      [DataType(DataType.Text)] 
      public string LastName { get; set; } 

      [DataMember] 
      [StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")] 
      [DataType(DataType.Text)] 
      public string Address1 { get; set; } 

      [DataMember] 
      [StringLength(50)] 
      [DataType(DataType.Text)] 
      public string Address2 { get; set; } 

      [DataMember] 
      [StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")] 
      [DataType(DataType.Text)] 
      public string City { get; set; } 

      [DataMember] 
      [StringLength(2, MinimumLength = 2, ErrorMessage = "Must have a length of 2.")] 
      [DataType(DataType.Text)] 
      public string State { get; set; } 

      [DataMember] 
      [StringLength(10)] 
      [DataType(DataType.Text)] 
      [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")] 
      public string Zip { get; set; } 

      [DataMember] 
      [StringLength(12)] 
      [DataType(DataType.PhoneNumber)] 
      [RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")] 
      public string HomePhone { get; set; } 

      [DataMember] 
      [StringLength(12)] 
      [DataType(DataType.PhoneNumber)] 
      [RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")] 
      public string CellPhone { get; set; } 

      [DataMember] 
      [StringLength(100)] 
      [DataType(DataType.Url)] 
      [Url] 
      public string Website { get; set; } 

      [DataMember] 
      [StringLength(50)] 
      [DataType(DataType.EmailAddress)] 
      [Email] 
      public string IMAddress { get; set; } 

      [DataMember] 
      public System.DateTime CreatedDate { get; set; } 

      [DataMember] 
      public Nullable<System.DateTime> UpdatedDate { get; set; } 

      [DataMember] 
      public virtual ICollection<Project> Projects { get; set; } 
     } 
    } 
} 

我的web服务方法如下:

public IEnumerable<Customer> GetCustomers() 
     { 
      YeagerTechEntities DbContext = new YeagerTechEntities(); 

      DbContext.Configuration.ProxyCreationEnabled = false; 

      IQueryable<Customer> customer = DbContext.Customers.Where(p => p.CustomerID > 0); 

      CloseConnection(DbContext); 

      return customer; 
     } 

我的客户方法进行调用以上述服务方法具有以下:

IEnumerable<YeagerTechModel.Customer> customerList = db.GetCustomers(); 

        return View(new GridModel<YeagerTechModel.Customer> 
        { 
         Data = customerList 
        }); 

我的观点如下:

@model Telerik.Web.Mvc.GridModel<YeagerTechModel.Customer> 
@{ 
    ViewBag.Title = "Customer Index"; 
} 
<h2> 
    Customer Index</h2> 
@( Html.Telerik().Grid<YeagerTechModel.Customer>(Model.Data) 
     .Name("Customers") 
      .DataKeys(dataKeys => dataKeys.Add(o => o.CustomerID) 
              .RouteKey("CustomerID")) 
       .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" })) 
     .Columns(columns => 
      { 
       columns.Bound(o => o.CustomerID).Hidden(true); 
       columns.Command(commands => 
       { 
        commands.Edit().ButtonType(GridButtonType.Text); 
       }).Width(200).Title("Command"); 
       columns.Bound(o => o.Email).Width(200).Filterable(false); 
       columns.Bound(o => o.Company).Width(200).Filterable(false); 
       columns.Bound(o => o.FirstName).Width(100).Title("FName").Filterable(false); 
       columns.Bound(o => o.LastName).Width(100).Title("LName").Filterable(false); 
       columns.Bound(o => o.Address1).Width(200).Title("Addr1").Filterable(false).Sortable(false); 
       columns.Bound(o => o.Address2).Width(100).Title("Addr2").Filterable(false).Sortable(false); 
       columns.Bound(o => o.City).Width(100); 
       columns.Bound(o => o.State).Width(40).Title("ST"); 
       columns.Bound(o => o.Zip).Width(60); 
       columns.Bound(o => o.HomePhone).Width(120).Filterable(false).Sortable(false); 
       columns.Bound(o => o.CellPhone).Width(120).Filterable(false).Sortable(false); 
       columns.Bound(o => o.Website).Width(100).Filterable(false).Sortable(false); 
       columns.Bound(o => o.IMAddress).Width(100).Filterable(false).Sortable(false); 
       columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false); 
       columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false); 
      }).DataBinding(dataBinding => 
       dataBinding.Ajax() 
         .Insert("_InsertAjaxEditing", "Customer") 
         .Update("_SaveAjaxEditing", "Customer")) 
    .Editable(editing => editing.Mode(GridEditMode.InLine)) 
    .Pageable() 
    .Sortable() 
    .Filterable() 
    .Scrollable() 
) 

回答

0

您可以修改T4模板以在生成类时包含属性。

您也可以考虑使用数据传输对象并注释它们而不是实体。我目前正在使用EF 4.1(db first)和WCF开发一个项目,并使用AutoMapper来映射我的DTO和实体。我的服务api获取并返回DTO,然后将这些DTO映射到存储库中的实体。这使我可以对服务公开的数据进行整形,当我不希望它看起来像存储在数据库中一样。

有关使用DTO的优点和缺点的更多讨论,请参阅此MSDN article