Entity framework 如何在不影响所有其他模型类的情况下更新.edmx文件?

Entity framework 如何在不影响所有其他模型类的情况下更新.edmx文件?,entity-framework,ado.net,Entity Framework,Ado.net,如何在不影响所有其他模型类的情况下更新.edmx文件 当我更新edmx文件时,它会重新创建所有其他类,这对我来说是个问题,因为我对其他类做了一些更改,因此如何修改它而不影响其他类 例如,这是我的一门课 public partial class company { public company() { this.Departments = new HashSet<department>(); this.CustomersOfTheComp

如何在不影响所有其他模型类的情况下更新
.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; }
}
公共部分类公司
{
上市公司()
{
this.Departments=newhashset();
this.customersofcompany=new HashSet();
this.companiesthecompanycustomer for=new HashSet();
this.CustomerProjectDetails=new HashSet();
this.CompanyAsSupplier=new HashSet();
this.CompanyAsCustomer=new HashSet();
this.Tickets=newhashset();
}
[必需(ErrorMessage=“需要公司名称”)]
[StringLength(200,MinimumLength=3,ErrorMessage=“公司名称的长度应超过三个字母”)]
公共字符串CompanyName{get;set;}
[必需]
[电子邮件地址(ErrorMessage=“无效电子邮件地址”)]
公共字符串电子邮件{get;set;}
[必需]
公共整数国家{get;set;}
public int company_id{get;set;}
公共字符串标志{get;set;}
公共字符串说明{get;set;}
私有CompanyDTO_CDTO=新CompanyDTO();
public CompanyDTO CDTO{get{返回这个。_CDTO;}设置{this。_CDTO=value;}}
公共虚拟ICollection部门{get;set;}
公司{get;set;}的公共虚拟国家/地区
公司{get;set;}的公共虚拟ICollection客户
公共虚拟ICollection公司{get;set;}的公司客户
公共虚拟ICollection CustomerProjectDetails{get;set;}
公共虚拟ICollection公司供应商{get;set;}
公共虚拟ICollection公司客户{get;set;}
公共虚拟ICollection票证{get;set;}
}

因此,当我修改.edmx时,类属性将不再可用。

重新生成生成的文件时,无法保留对生成文件的编辑。如果您需要将属性应用于生成的代码,那么有一种机制允许您在另一个分部类中指定验证属性


有关这方面的更多信息,请参阅或。

我不理解您的示例,但我将在这里解释:

假设您有以下实体模型:

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; }
}
无论何时创建新用户对象,您都将看到新属性:

同样,您也可以对
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; }
}
然后


最好使用分部类来生成changes@DawoodAwan我编辑了我的示例,因此如何保存修改后的类属性答案的另一部分是,如果要向模型类添加其他属性或方法以确保其安全,则必须在单独的文件中创建另一个分部类更新
edmx
文件时删除
Extensions.cs:

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

// Similarly you can do

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