Can I abstract Entity Framework away from my Entities?

前端 未结 5 1297
野性不改
野性不改 2021-02-08 16:03

I have a Foo entity in Entity Framework. But I\'m making it inherit from IFoo so that my business logic only knows IFoo - thus abstracting

5条回答
  •  庸人自扰
    2021-02-08 16:31

    Use a partial class to seperate your logic and rules from the autogenerated EF objects. In the example below FooEntityObject class is split into two using the partial keyword. I've used this technique before with EF and LINQ to SQL. The partial classes can be stored in seperate files so if your regenerate your EF object again your custom code doesn't get overwriten.

    interface IFoo
    {
        public ICollection GetBars();
    }
    
    public partial class FooEntityObject : IFoo
    {
        public ICollection GetBars()
        {
            // convert EntityCollection into ICollection here
        }
    }
    
    
    public partial class FooEntityObject
    {
        EntityCollection Bars{get;set;}
    }
    

提交回复
热议问题