locked
How can I inherit ADO entity class? RRS feed

  • Question

  • when I create a new class which inherit a entity class,a error occured that: can not inherit a seated class.

    How can I inherit ADO entity class?

    Tuesday, January 11, 2011 10:43 AM

Answers

  • Both parts of the partial classes need to be in the same project, so that the compiler can merge them into a single class. In your project, your extending class is in a seperate project, so the compiler thinks they should be seperate, but then it notices that they conflict.

    Try putting your partial class extension in the ServiceLibrary project.

    Wednesday, January 12, 2011 1:22 AM

All replies

  • Hi. As you've already found out, you can't. But there are other possibilities, and those classes are partial and also have partial methods you can use for extensibility. Can you tell us what you want to do?

    Tuesday, January 11, 2011 11:22 AM
  • Your question is missing some details (inheritance in the Web project or the Silverlight project? what is the goal that you are trying to accomplish?), but I'll guess that you have your WCF RIA Services project set up, you are using Entity Framework on the server, you have a domain service, and you have been able to the the client entities generated in your Silverlight client. Then you want to inherit from those generated entities, to provide some extra functionality, but discovered that the generated entities are sealed.

    In the past, I too have wanted the generated entities to be unsealed, but the RIA team would have have to spend extra time to make sure that feature worked correctly, time which was used to make other main-stream scenarios more solid, and add other features.

    That said, there are ways to extend the generated entities. They are generated with the "partial" modifier, so in your code in the client, you can add extra methods to the class, or implement the partial method stubs in the code. Each entitiy provides an OnCreated method that you can implement, which is called when the entitiy is created. Also, for each property, there are two methods, OnPropertyNameChanging(proprtyType value), and OnPropertyNameChanged, which get called at different points in the property setters.

    To make this more concrete, in a sample project I'm working, I have a Config entitiy on the server, which has an integer-typed Id property. However, on the client, I want to expose that Id as a enum-typed Group property. Because the generated Config entity is a partial class, I can add a new file to my client project, that extends the original definition by adding a Group property (which exposes the main entity's Id property). To get XAML data-binding to work correctlly, I also provide an implementation for the OnIdChanged partial method (which is defined in the generated code). Here is the code:

        public partial class Config {
            public Groups Group {
                get { return (Groups)this.Id; }
                set {
                    this.Id = (int)value;
                    this.RaisePropertyChanged("Group");
                }
            }
           // This partial method extends what
           // is declared in the generated code.
           // The Id property setter called this code.

            partial void OnIdChanged() {
                this.RaisePropertyChanged("Group");
            }
    }

    I hope that helps clear thing up.

    Tuesday, January 11, 2011 11:46 AM
  • Hi, The partial method , I've tried .

    entity class SI_Company ,SI_BusinessUnits,aspnet_Users

    In Organization.cs file:

        public partial class aspnet_Users
        {
            public string Path { get; set; }
        }

        public partial class SI_BusinessUnits
        {
            public string Path { get; set; }
            public ObservableCollection<Object> Items { get; set; }
        }

        public partial class SI_Companies
        {
            public string Path { get; set; }
            public ObservableCollection<SI_BusinessUnits> Items { get; set; }
        }

    the error : there is a conflict between SI_BusinessUnits in generated entities file and in Organization.cs file.

    How can I do?

    Wednesday, January 12, 2011 12:01 AM
  • What is the exact text of the error message? My guess is that either the Path or Items is alreadt defined in the generated entities file.

    Wednesday, January 12, 2011 12:23 AM
  • the error message :

    Warning:

         The type 'Project7.ServiceLibrary.Web.SI_BusinessUnit' in 'E:\Project7.Infrastructure\Orgnization\Organization.cs ' conflicts with the imported type 'Project7.JSJS.OA.ServiceLibrary.Web.SI_BusinessUnits' in 'E:\Project7.ServiceLibrary\Bin\Debug\Project7.ServiceLibrary.dll'. Using the type defined in 'E:\Project7.Infrastructure\Organization\Organization.cs'

    Wednesday, January 12, 2011 1:00 AM
  • Both parts of the partial classes need to be in the same project, so that the compiler can merge them into a single class. In your project, your extending class is in a seperate project, so the compiler thinks they should be seperate, but then it notices that they conflict.

    Try putting your partial class extension in the ServiceLibrary project.

    Wednesday, January 12, 2011 1:22 AM
  • Hi.

    Now,I put Organization.cs in the ServiceLibrary Project.

    But ,In the client,I can not visit my new element(like path).

    Wednesday, January 12, 2011 1:54 AM
  • From what you've said, the Path should be visible. Now you are in the realm of C# coding in Visual Studio, so you can use the normal debugging tricks, looking at things like: is a recompile needed? Are any properties you add visble? How about methods or fields? Are the visibility modifiers that need to change "internal", "protected", etc.? Can you add other new classes in the library and have them be visible?

    Good luck!

    Wednesday, January 12, 2011 2:30 PM