简体   繁体   English

.NET 框架 4 上 ASP.NET MVC 5 实体框架 6 上的扩展方法

[英]Extension method on Entity Framework 6 in ASP.NET MVC 5 on the .NET Framework 4

I'm customizing an old web application, built on ASP.NET MVC 5, .NET 4.6, Entity Framework 6.我正在定制一个旧的 web 应用程序,它建立在 ASP.NET MVC 5、.NET 4.6、实体框架 6 上。

I used Entity Framework to built it with a database-first approach.我使用实体框架以数据库优先的方法构建它。 I have not used DDD / Repository / Services layer (it's a simple architecture).我没有使用 DDD / Repository / Services 层(这是一个简单的架构)。

I need to do this:我需要这样做:

  • I don't want to massively update database records我不想大量更新数据库记录
  • Every time that I create/edit/list a PERSON_ENTITY item or dataset, I run a method on it每次我创建/编辑/列出一个PERSON_ENTITY项目或数据集时,我都会在其上运行一个方法
  • For example, turn to upper case FIRSTNAME and LASTNAME properties/fields or round DAYS_FROM_LAST_LOGIN例如,转为大写FIRSTNAMELASTNAME属性/字段或舍DAYS_FROM_LAST_LOGIN
  • I don't want duplicate code in create/edit/list action of PERSON_ENTITY controller我不想在PERSON_ENTITY controller 的创建/编辑/列表操作中重复代码
namespace Webapplication4.Controllers
{
    [Authorize]
    public class PersonsController : Controller
    {
        private CS_Webapplication4_Entities db = new CS_Webapplication4_Entities();
 
        public ActionResult Index()
        {
            var myDataset = db.PERSON_ENTITY ;
            
            //----------------------------------** 1° point **
            foreach(PERSON_ENTITY myPerson in myDataset)
            {
                myPerson.Firstname = Utils.Upperize(myPerson.Firstname);
                myPerson.Lastname = Utils.Upperize(myPerson.Lastname);
            }

            return View(myDataset.ToList());
        }
 
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            PERSON_ENTITY myPerson = db.PERSON_ENTITY.Find(id);

            if (myPerson == null)
            {
                return HttpNotFound();
            }

            ////---------------------------------- 2° point
            myPerson.Firstname = Utils.Upperize(myPerson.Firstname);
            myPerson.Lastname = Utils.Upperize(myPerson.Lastname);

            return View(myPerson);
        }
        
        public ActionResult Create()
        {
            //...
            return View();
        }
         
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "...")] PERSON_ENTITY myPerson)
        {
            if (ModelState.IsValid)
            {
                //3° point
                myPerson.Firstname = Utils.Upperize(myPerson.Firstname);
                myPerson.Lastname = Utils.Upperize(myPerson.Lastname);

                db.PERSON_ENTITY.Add(myPerson);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            //...
            return View(myPerson);
        }
         
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            PERSON_ENTITY myPerson = db.PERSON_ENTITY.Find(id);

            if (myPerson == null)
            {
                return HttpNotFound();
            }

            //...
            //4° point
            myPerson.Firstname = Utils.Upperize(myPerson.Firstname);
            myPerson.Lastname = Utils.Upperize(myPerson.Lastname);

            return View(myPerson);
        }
         
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "...")] PERSON_ENTITY myPerson)
        {
            //5° point
            myPerson.Firstname = Utils.Upperize(myPerson.Firstname);
            myPerson.Lastname = Utils.Upperize(myPerson.Lastname);

            if (ModelState.IsValid)
            {
                db.Entry(myPerson).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            //...
            
            return View(myPerson);
        }
    }
}

I read that is possible to extend Entity Framework, by adding some extension methods.我读到可以通过添加一些扩展方法来扩展实体框架。 If that's possible, I can write something like this:如果可能的话,我可以这样写:

public static AppIdentityDbContext MyExtension()  
{
    var MyCustomizedMethod = new SOMETHING() {
                    if EntityType.Equals(PERSON_ENTITY)
                    {
                        PERSON_ENTITY.Firstname = Utils.Upperize(myPerson.Firstname);
                        PERSON_ENTITY.Lastname = Utils.Upperize(myPerson.Lastname);
                    }
                
    };

    return new AppIdentityDbContext().AddSomething(MyCustomizedMethod);;
}

Please, can someone help me to do this?拜托,有人可以帮我做到这一点吗?

Is it possible to extend Entity Framework as outlined above?是否可以如上所述扩展实体框架?

Thanks to all谢谢大家

Well, you've almost done it.好吧,你几乎做到了。 First deifine a static class for the context's extensions and add your new custom extension method首先为上下文的扩展定义一个 static class 并添加新的自定义扩展方法

 public static class AppIdentityDbContextExtensions()  
 {
    public static bool AddSomething(this AppIdentityDbContext appIdentityDbContext )
    {
        <your-code-here>
    };
 }

And then call the new extension method like this然后像这样调用新的扩展方法

    return new AppIdentityDbContex().AddSomething();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM