locked
Where to add entity property for EF based entity ? RRS feed

  • Question

  • I have a domain service based off an Entity Framework model which has an Entity "Book". If I want to add a property "XXX" to book and XXX is not in the EF model but rather obtained from somplace else, assuming the code to support the new property is something simple like this:

        private int _XXX;
        public int XXX {
            get { return _XXX; }
            set { _XXX = value; }
        }
    

    Should that code go in a file that extends the partial class the EF defined (e.g. "public partial class Book : global::System.Data.Objects.DataClasses.EntityObject" ) ?

    Or should that go in the BookService.metadata.cs in the "public partial class Book" right before the metadata class declaration, thus modifying the domain service Book class ?

    Or someplace else ?

     

    Monday, April 20, 2009 4:58 PM

Answers

  • You can put it in either one. Just make sure you don't put in the file that's auto-generated by the EF, so that it will not be overwirtten if you update your model...

    Monday, April 20, 2009 5:05 PM
  • Should that code go in a file that extends the partial class the EF defined (e.g. "public partial class Book : global::System.Data.Objects.DataClasses.EntityObject" ) ?

    Or should that go in the BookService.metadata.cs in the "public partial class Book" right before the metadata class declaration, thus modifying the domain service Book class ?

    Or someplace else ?

    It doesn't really matter, but I would create a new file instead of reusing the one in the metadata file just for consistancy. The important thing to understand is that they are the exact same class.

     What is confusing you is that you only need to list the base class in one of the partial class files, the other files just list the class name itself. So, even though the generated file says public partial class Book : global::System.Data.Objects.DataClasses.EntityObject and the metadata.cs file just says public partial class Book they are really the same partial class. So, when you create a seperate file for your own code you would just say public partial class Book.

    Monday, April 20, 2009 5:28 PM

All replies

  • You can put it in either one. Just make sure you don't put in the file that's auto-generated by the EF, so that it will not be overwirtten if you update your model...

    Monday, April 20, 2009 5:05 PM
  • Should that code go in a file that extends the partial class the EF defined (e.g. "public partial class Book : global::System.Data.Objects.DataClasses.EntityObject" ) ?

    Or should that go in the BookService.metadata.cs in the "public partial class Book" right before the metadata class declaration, thus modifying the domain service Book class ?

    Or someplace else ?

    It doesn't really matter, but I would create a new file instead of reusing the one in the metadata file just for consistancy. The important thing to understand is that they are the exact same class.

     What is confusing you is that you only need to list the base class in one of the partial class files, the other files just list the class name itself. So, even though the generated file says public partial class Book : global::System.Data.Objects.DataClasses.EntityObject and the metadata.cs file just says public partial class Book they are really the same partial class. So, when you create a seperate file for your own code you would just say public partial class Book.

    Monday, April 20, 2009 5:28 PM
  • Make sense, I was just wondering what conventions people were using. Now with this and the rest of the RIA files my Book class could be defined across many files now including:

    1. BookModel.Designer.cs (EF generated)
    2. Book.shared.cs
    3. BookService.metadata.cs
    4. Book.MyExtensions.cs (or some such name, this being the file we're talking about adding with my XXX property)

     

    It's a lot of  files to search down a property or method definition in but VS makes it easy enough to find so I guess it's not a big deal

    Monday, April 20, 2009 5:42 PM
  • Well, just to add to your conventions then, I would typically name #4 just Book.cs and would put it in a seperate folder along with all of the other entity partial classes.

    Monday, April 20, 2009 5:54 PM