2015-08-27 38 views
1

如何在不影響所有其他模型類的情況下更新.edmx文件?如何在不影響所有其他模型類的情況下更新.edmx文件?

當我更新edmx文件時,它會重新創建所有其他類,這對我來說是個問題,因爲我對其他類進行了一些更改,因此如何在不影響其他類的情況下對其進行修改。

例如,這是我的課

public partial class company 
{ 
    public company() 
    { 
     this.Departments = new HashSet<department>(); 
     this.CustomersOfTheCompany = new HashSet<company_customers>(); 
     this.CompaniesTheCompanyCustomerFor = new HashSet<company_customers>(); 
     this.CustomerProjectDetails = new HashSet<cus_pro_connector>(); 
     this.CompanyAsSupplier = new HashSet<company_suppliers>(); 
     this.CompanyAsCustomer = new HashSet<company_suppliers>(); 
     this.Tickets = new HashSet<oneTimeAuthenticationTicket>(); 
    } 
    [Required(ErrorMessage = "Company name is required")] 
    [StringLength(200, MinimumLength = 3, ErrorMessage = "Length Of The Company Name Should Be More Than Three Letters")] 
    public string CompanyName { get; set; } 

    [Required] 
    [EmailAddress(ErrorMessage = "Invalid Email Address")] 
    public string Email { get; set; } 

    [Required] 
    public int Country { get; set; } 

    public int company_id { get; set; } 
    public string Logo { get; set; } 
    public string Description { get; set; } 
    private CompanyDTO _CDTO = new CompanyDTO(); 
    public CompanyDTO CDTO { get { return this._CDTO; } set { this._CDTO = value; } } 

    public virtual ICollection<department> Departments { get; set; } 
    public virtual country CountryOfTheCompany { get; set; } 
    public virtual ICollection<company_customers> CustomersOfTheCompany { get; set; } 
    public virtual ICollection<company_customers> CompaniesTheCompanyCustomerFor { get; set; } 
    public virtual ICollection<cus_pro_connector> CustomerProjectDetails { get; set; } 
    public virtual ICollection<company_suppliers> CompanyAsSupplier { get; set; } 
    public virtual ICollection<company_suppliers> CompanyAsCustomer { get; set; } 
    public virtual ICollection<oneTimeAuthenticationTicket> Tickets { get; set; } 
} 

所以當我修改的.edmx類屬性將不再可用之一。

+0

這是更好地使用部分類進行更改 –

+0

@DawoodAwan我編輯我的例子,所以我怎麼能保存類從修改屬性 – user3260672

+1

答案的另一部分是你必須在一個單獨的文件中創建另一個分類,如果你想添加額外的屬性或方法到模型類,以確保它們在更新'edmx'文件時可以安全移除 – Moh

回答

3

重新生成文件時,無法保留對生成文件的編輯。如果您需要將屬性應用於生成的代碼,則可以使用MetadataType機制來指定另一個分部類中的驗證屬性。

請參閱this other answerMSDN瞭解更多信息。

0

我不明白你的榜樣 - 但我會在這裏解釋:

比方說,你有以下實體模型:

enter image description here

而且User.cs看起來是這樣的:

public partial class User 
{ 
    public User() 
    { 
     this.UserWindows = new HashSet<UserWindow>(); 
    } 

    public int UserId { get; set; } 
    public string UserName { get; set; } 
    public string Password { get; set; } 

    public virtual ICollection<UserWindow> UserWindows { get; set; } 
} 

添加一個新文件並將其命名爲Extensions.cs,然後在此文件中創建一個新的部分類:

enter image description here

然後你就可以添加屬性到這個類:

public partial class User 
{ 
    public int NewUserId { get; set; } 
} 

,每當你創建一個新的用戶對象,你會看到你的新屬性:

enter image description here

同樣你可以這樣做UserWindow.cs

Extensions.cs: 

public partial class User 
{ 
    public int NewUserId { get; set; } 
} 

// Similarly you can do 

public partial class UserWindow 
{ 
    public string MyNewProperty { get; set; } 
} 

然後

enter image description here

相關問題