数据库优先 - DbModelBuilder或从不支持的DbContext编写EDMX [英] Database First - DbModelBuilder or writing the EDMX from a DbContext not supported

查看:107
本文介绍了数据库优先 - DbModelBuilder或从不支持的DbContext编写EDMX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我首先使用数据库作为我的MVC项目。我有我的模特。当我想对我的模型进行查询时,我面临一个错误:

I use database first for my MVC project. I have my model. when I want to make a query on my model I face an error:


DbModelBuilder或从不支持的DbContext写入EDMX。

DbModelBuilder or writing the EDMX from a DbContext not supported.


MyModel_Entities上下文;

MyModel_Entities context;

    public RepositoryBType()
    {
        context = new MyModel_Entities();
    }

    public IList<Models.BType> GetAll()
    {
        return context.BType.ToList();
    }



它显示返回context.BType.ToList();

It shows an error on return context.BType.ToList();


不支持从使用Database First或Model First创建的DbContext创建DbModelBuilder或编写EDMX。只能从不使用现有DbCompiledModel创建的Code First DbContext获取EDMX。

Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.


感谢您的帮助。

Thanks for any help.

推荐答案

IMO,您应该放弃存储库模式,因为您获取异常消息。我自己,我使用分层样式在ASP.NET MVC解决方案中首先使用DAO模式和带有EF DB的DTO模式。

IMO, you should abandon the Repository pattern, since you are  getting the exception message. Myself, I use the DAO pattern along with the DTO pattern with EF DB first in an ASP.NET MVC solution using a layered style.

表示层MVC项目,服务 图层classlib项目和数据访问层,DAO用于DAL classlib项目。

Presentation Layer the MVC project, Service  Layer classlib project  and Data Access Layer with DAO being used in the DAL classlib project.

https://docs.microsoft.com/ en-us / previous-versions / msp-np / ee658117(v = pandp.10)

这是关于一般的存储库模式,特别是通用存储库并避免它。

This is about the repository pattern in general, particularly the generic repository and avoiding it.

https://martinfowler.com/eaaCatalog/repository.html

https://blog.sapiensworks.com/post/2012/03/05/The-Generic-Repository-Is-An-Anti-Pattern.aspx

https://programmingwithmosh.com/entity-framework/common-mistakes-with-the-repository-pattern/

https://www.infoworld.com/article/3117713/application-development/design-patterns-that-i-often-avoid-repository-pattern.html

https://blog.sapiensworks.com/post/2012/11/01/Repository-vs-DAO.aspx

当它被正确用作域/业务解决方案而不是数据库持久性解决方案时,我没有反对存储库模式。

I have nothing against the repository pattern when it is used properly as a Domain/Business solution and not a database persistence solution.

对我而言,无论我使用的是MVC,MVP还是MVVM,服务层都将两层的演示文稿和数据分开。 

For me no matter if I am using MVC,  MVP or MVVM. the service layer separates the two layers presentation and data. 

  https://www.red-gate.com/simple-talk/dotnet/net-development/building-better-entity-framework-applications/

https://en.wikipedia.org/wiki/Data_access_object

https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm

https:// www .codeproject.com / Articles / 1050468 / Data-Transfer-Object-Design-Pattern-in-Csharp

使用带有EF的DAO模式的DAL示例首先是数据库,或者它可能是隐藏在DAL中的代码,所有项目都引用了保存通过层发送的DTO的实体proect。

Example of DAL using the DAO pattern with EF DB first or it could be code first that is buried in the DAL and all projects have reference to the Entities proect that holds the DTO(s) that are sent through the layers.

using System;
using System.Collections.Generic;
using Entities;

namespace DAL.DAO
{
    public interface IDAOStudent
    {
        DTOStudent GetStudentById(Int32 id);
        List<DTOStudent> GetStudents();
        void CreateStudent(DTOStudent dto);
        void UpdateStudent(DTOStudent dto);
        void DeleteStudent(Int32 id);
    }
}

==============================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using Entities;
using DAL.Model;

namespace DAL.DAO
{
    public class DAOStudent : IDAOStudent
    {
        public DTOStudent GetStudentById(Int32 id)
        {
            var dto = new DTOStudent();
            using (var context = new CUDataEntities())
            {
                var student = (context.Students.Where(a => a.StudentID == id)).SingleOrDefault();

                if (student != null)
                {
                    dto.StudentID = student.StudentID;
                    dto.FirstName = student.FirstName;
                    dto.LastName = student.LastName;
                    dto.EnrollmentDate = student.EnrollmentDate;

                    var enrolllments =  new DAOEnrollment().GetEntrollmentsByStudentId(id).ToList();
                    var courses = new DAOCourse().GetCoursesByStudentCourseId(student.StudentID).ToList();

                    dto.EnrollsandCourses = (from a in enrolllments
                                  join b in courses on a.CourseID equals b.CourseID
                    select new  DTOEnrollandCourse()
                     { Title = b.Title, Credits = b.Credits, Grade = a.Grade }).ToList();
                }
            }

            return dto;
        }
        public void CreateStudent(DTOStudent dto)
        {
            using (var context = new CUDataEntities())
            {
                var student = new Student
                {
                    FirstName = dto.FirstName,
                    LastName = dto.LastName,
                    EnrollmentDate = dto.EnrollmentDate
                };

                context.Students.Add(student);
                context.SaveChanges();
            }
        }

        public void DeleteStudent(int id)
        {
            Student student;
            using (var context = new CUDataEntities())
            {
                student = (context.Students.Where(a => a.StudentID == id)).SingleOrDefault();
            }

            using (var newContext = new CUDataEntities())
            {
                newContext.Entry(student).State = System.Data.Entity.EntityState.Deleted;
                newContext.SaveChanges();
            }
        }

        public List<DTOStudent> GetStudents()
        {
           
            var dtos = new List<DTOStudent>();

            using (var context = new CUDataEntities())
            {
                //var adapter = (IObjectContextAdapter)context;
                //var objectContext = adapter.ObjectContext;
                
                //var entityConn = objectContext.Connection as EntityConnection;
                //var dbConn = entityConn.StoreConnection as SqlConnection;

                //dbConn.Open();

                var students = context.Students.ToList();

                foreach(var stud in students)
                {
                    var dto = new DTOStudent
                    {
                        StudentID = stud.StudentID,
                        FirstName = stud.FirstName,
                        LastName = stud.LastName,
                        EnrollmentDate = stud.EnrollmentDate
                    };

                    dtos.Add(dto);
                }
            }

            return dtos;
        }

        public void UpdateStudent(DTOStudent dto)
        {
            var student = new Student();

            using (var context = new CUDataEntities())
            {
                student = (context.Students.Where(a => a.StudentID == dto.StudentID)).SingleOrDefault();
            }

            if (student != null)
            {
                student.FirstName = dto.FirstName;
                student.LastName = dto.LastName;
                student.EnrollmentDate = dto.EnrollmentDate;
            } 

            using (var dbcontext = new CUDataEntities())
            {
                if (student != null)
                {
                    dbcontext.Entry(student).State = EntityState.Modified;
                    dbcontext.SaveChanges();
                }
            }
        }
    }
}





using System;
using System.Collections.Generic;

namespace Entities
{
    public class DTOStudent
    {
        public Int32 StudentID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public DateTime? EnrollmentDate { get; set; }

        public virtual ICollection<DTOEnrollandCourse> EnrollsandCourses { get; set; }
    }
}





这篇关于数据库优先 - DbModelBuilder或从不支持的DbContext编写EDMX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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