mohaidar
mohaidar

Reputation: 4251

how to update .edmx file without effecting all the other model classes?

how to update .edmx file without effecting all the other model classes?

when I update the edmx file it recreates all the other classes and this is a problem for me because I made some changes on the other classes so how can I modify it without affecting the other classes.

For example this is one of my classes

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; }
}

so when I modify the .edmx the class attributes will no longer be available.

Upvotes: 1

Views: 203

Answers (2)

Dawood Awan
Dawood Awan

Reputation: 7318

I don't understand your example - But I will explain here:

Let's say you have the following Entity Models:

enter image description here

And the User.cs looks like this:

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; }
}

Add a new file and call it Extensions.cs, then create a New Partial Class in this File:

enter image description here

Then you can Add Properties to this Class:

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

And whenever you create a New User Object you will see your new Properties:

enter image description here

Similarly you can do this for 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; }
}

Then

enter image description here

Upvotes: 0

Squiggle
Squiggle

Reputation: 2597

It is not possible to retain edits to generated files when you re-generate them. If you need to apply attributes to generated code, there is a MetadataType mechanism that allows you to specify validation attributes in another partial class.

See this other answer or MSDN for further information on this.

Upvotes: 2

Related Questions