【发布时间】:2011-11-05 22:30:11
【问题描述】:
我有一个使用数据库优先方法生成的 EF 模型。在设计器上,我使用 DBContext 对象创建了一个“代码生成项”。这创建了一个为我的表(我想要的)生成 POCO 类的模板。
我一直在使用这些相同的类在 WCF 和其中的 DataAnnotation 属性中进行编码,效果很好。我保存了这个文件的副本,如果模型重新生成,我只需将旧代码粘贴到新创建的模型生成类中并更新任何新属性。
我试图更进一步。每当我更改模型时,类都会重新生成并且属性会丢失。我试图做的是在我的项目中创建一个单独的文件夹,该文件夹具有相同的类名,通过命名空间限定它。我基本上会将生成的 POCO 类中更改的任何属性复制到我创建的新文件夹中的另一个类,并简单地添加我需要的任何属性。但是,这几乎与上面第二段中的相同。但是,如果有大型数据库模型,这可能会变得乏味且容易出错。
我想要做的是在不丢失属性的情况下以某种方式模拟生成模型。是的,我知道 --- 有我的蛋糕,也吃它......
有什么想法吗?
我尝试添加好友课程,但没有成功。
数据可以正常传输,但在添加好友类后,DataAnnotations 不起作用。
我想也许我需要更改我的服务方法以包含 Customer_Validation 对象而不是 Customer 并为客户做同样的事情。
我正要进行此更改,但在我的服务方法中遇到了以下代码 sn-p 的障碍(这甚至在我更改了 DbContext 的定义之后当然是另一个代码生成的类。)。 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()
)
【问题讨论】:
标签: wcf entity-framework-4.1 data-annotations