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

时间:2015-08-27 11:20:33

标签: entity-framework ado.net

如何在不影响所有其他模型类的情况下更新.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时,类属性将不再可用。

2 个答案:

答案 0 :(得分:3)

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

有关详情,请参阅this other answerMSDN

答案 1 :(得分: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

相关问题