使实体框架实现接口 [英] Making Entity framework implement an interface

查看:112
本文介绍了使实体框架实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用IoC和实体框架和Ninject。我想我需要生成的实体类来实现一个接口,ICRUD。有一个<一个href=\"http://blogs.msdn.com/b/efdesign/archive/2009/01/22/customizing-entity-classes-with-t4.aspx\">walkthrough它展示了如何强制实体框架,以实现一个接口。我跟着方向和我的EntityObject codeGenerator.cs文件确实显示ICrud,但没有实现的接口。我看不出在EntityObject codeGenerator.tt任何子类的文章说我应该。我得到错误

I want to use IoC with Entity framework and Ninject. I figure I need the Generated Entity classes to implement an interface, ICRUD. There's a walkthrough that shows how to force Entity framework to implement an interface. I followed the directions and my EntityObjectCodeGenerator.cs file indeed shows "ICrud", but doesn't implement the interface. I don't see any subclasses under EntityObjectCodeGenerator.tt as the article says I'm supposed to. I get error

BugnetMvc.Models.BugNetEntities'呢
  未实现接口成员
  BugnetMvc.Services.ICrud.Update()

'BugnetMvc.Models.BugNetEntities' does not implement interface member 'BugnetMvc.Services.ICrud.Update()'

更新

我们的目标是创建一个可测试的,可扩展的MVC-3利用内联网实体框架,还支持强类型的视图和局部视图。从我的使用经验Ninject小级别迄今为止,我相信我需要重载我的控制器的构造与视图本身即服务(假设提供每个接口CRUD方法)每个部分的视图和一个:

UPDATE
The goal is to create a testable, extensible MVC-3 intranet utilizing entity framework that also supports strongly typed Views and partial views. From my small level of experience with Ninject thus far, I believe I need to overload my Controller's constructor with a service for the View itself (Assume CRUD methods available for each interface) and one for each partial view:

例如

public HomeController(HomeService homeCrudService, PartialViewService1 partialviewService)

UPDATE2

需要明确的是,希望帮助别人,在code可以实现如下:

Update2
To be clear and hopefully help others, the code can be implemented as follows:

这是怎么可以扩展实体

namespace BugnetMvc.Models//ensure namespace matches entity
{
    public partial class Milestone : ICrud<Milestone>//Entity, note the CRUD generic.  This gives us a lot of flexibility working with Ninject
    {
        public bool Create()
        {
            throw new System.NotImplementedException();
        }

        public List<Milestone> Read()
        {
            var milestones = new List<Milestone>();

            var result = from a in new BugNetEntities1().Milestones
                            where a.MilestoneID >= 0
                            select new { a.Milestone1 };

            milestones = result.AsEnumerable()
                                        .Select(o => new Models.Milestone
                                        {
                                            Milestone1 = o.Milestone1
                                        }).ToList();
            return milestones;
        }

        public bool Update()
        {
            throw new System.NotImplementedException();
        }

        public bool Delete()
        {
            throw new System.NotImplementedException();
        }
    }

一个样本模拟实体:

namespace BugnetMvc.Services
{
    public class MilestoneServiceMock : ICrud<MilestoneMock>
    {
        public MilestoneServiceMock()
        {

        }

        public bool Create()
        {
            throw new System.NotImplementedException();
        }

        public bool Update()
        {
            throw new System.NotImplementedException();
        }

        public bool Delete()
        {
            throw new System.NotImplementedException();
        }


        List<MilestoneMock> ICrud<MilestoneMock>.Read()
        {
            //string[] mileStones = new string[14];
            List<MilestoneMock> milestoneMocks = new List<MilestoneMock>();
            milestoneMocks.Add(new MilestoneMock("New"));
            milestoneMocks.Add(new MilestoneMock("Assessment"));
            milestoneMocks.Add(new MilestoneMock("Pending Approval"));
            milestoneMocks.Add(new MilestoneMock("Pending Start"));
            milestoneMocks.Add(new MilestoneMock("Planning"));
            milestoneMocks.Add(new MilestoneMock("Dev-In Process"));
            milestoneMocks.Add(new MilestoneMock("Dev-Pending Approval to QA"));
            milestoneMocks.Add(new MilestoneMock("Dev-Pending Move to QA"));
            milestoneMocks.Add(new MilestoneMock("QA-In Process"));
            milestoneMocks.Add(new MilestoneMock("QA-UAT"));
            milestoneMocks.Add(new MilestoneMock("QA-Pending Approval to Prod"));
            milestoneMocks.Add(new MilestoneMock("QA-Pending Move to Prod"));
            milestoneMocks.Add(new MilestoneMock("On-Going"));
            return milestoneMocks;
        }
    }
}
//Global.asax
        internal class SiteModule : NinjectModule
        {
            public override void Load()
            {
                bool MOCKDB = true;
                MOCKDB = false;
                if (MOCKDB)
                {
                    //Set up ninject bindings here.
                    Bind<ICrud<MilestoneMock>>().To<MilestoneServiceMock>();
                    Bind<ICrud<Application>>().To<ApplicationService>();
                }
                else
                {
                    //Set up ninject bindings here.
                    Bind<ICrud<Milestone>>().To<Milestone>();
                    Bind<ICrud<Application>>().To<ApplicationService>();
                }
            }
        }

对于需要读(INT ID)可能会出现,在这种情况下,用上述同样的基本思路一个新的接口应该做的伎俩。甚至可以更新ICrud到模型类型传递到方法为好。有多种选择。这为我工作,感谢乔恩斯基特他的专家指导。

The need for Read(int Id), may arise, in which case a new interface using the same basic ideas above should do the trick. One could even update ICrud to pass the model type into the methods as well. There's plenty of options. This worked for me, thanks to Jon Skeet for his expert guidance.

推荐答案

presumably生成的实体类是局部类,是否正确?

Presumably the generated entity classes are partial classes, correct?

如果是这样,你可以添加自己的部分类文件来指定要实现的接口 - 并提供您所需要的任何实际的实现方法。我猜想,会比改变被生成的内容简单多了。

If so, you can just add your own partial class files to specify the interfaces to be implemented - and to provide any actual implementation methods you need. I suspect that will be a lot simpler than changing what gets generated.

这篇关于使实体框架实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆