我有一個使用數據庫優先方法生成的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()
)