문제

I'm using EF 4.2 (database-first) with the DBContext T4 templates to create my POCO classes. This works very well, but now I stumpled over an issue with an abstract base class:

I need my T4 generated POCO class to inherit from a custom abstract class that has abstract properties:

//my abstract base class (shortened)
public abstract class BaseClass  {
    public abstract int? Property1 { get; set; }
    public abstract int? Property2 { get; set; }

    // a lot of methods that work with above properties
}

//my T4 generated entity (shortened)
public partial class Entity {
        public Nullable<int> Property1 { get; set; }
        public Nullable<int> Property2 { get; set; }
}

//and a partial class to make the T4 entity inherit from my abstract class
public partial class Entity : BaseClass {
}

My problem is that the compiler won't build this, because the properties in the T4 class are not marked as "override". LINQ 2 SQL had the option to change the inheritance modifier for an entity property, but I can't find this option in the EF Model Designer.

Is there a way to tell the EF Model Designer and the T4 templates to mark certain properties as override (I could, of course, change the generated C# code, but this changes would be overwritten when T4 runs again)? Is there any other way I can make this compile & work?

Thanks a lot, ~ saxx


Update 1: Fixed a typo.

도움이 되었습니까?

해결책

EF Model Designer offers no option to change the inheritence modifier. There are a few workarounds though.

  1. Build a convention based system in your T4 model where you react differently (add override keyword) when the name of a property matches a certain pattern.
  2. Use an interface rather than a baseclass and define logic in static classes/extension methods.
  3. Rename the relevant properties in the EF designer and optionally, set their access modifier on private. Implement the abstract properties by making them reflect the renamed properties.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top