开发者

Entity Framework 4 & Partial Class Inheritance

开发者 https://www.devze.com 2023-01-21 09:58 出处:网络
I havea simple EF Model with a Test entity and I want to make use of partial classes to add validation like this:

I havea simple EF Model with a Test entity and I want to make use of partial classes to add validation like this:

namespace WebApp.Model
{
    using WebApp.BusinessRules;
    using WebApp.BusinessRules.Rules;
    using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

    [HasSelfValidation]
    public partial class Test : BusinessObject
    {
        public Te开发者_开发技巧st()
        {
            AddRule(new ValidateRequired("Title"));
        }
    }
}

But I get this error:

Partial declarations of 'Model.Test' must not specify different base classes.

I understand the error but how can I use EF4 Models and still have access to all my business validation goodness?

BusinessObject has all the validation and stuff, so if I can get them working happily together, I'm all done. Hope someone can help.

Richard


With your current setup (using default EF EntityObjects) there is no way that you can get your entities inherit from BusinessObject. Instead of inheritance you can use Composition though, which means each partial class would have a field of BusinessObject type.

If you want to stick to your inheritance, then you should use EF 4 POCO Entitie instead and then have your POCOs to inherit from BusinessObject.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号