locked
Cant install Entity Framework RRS feed

  • Question

  • I would like to install a database first entity Framework dbcontext on my new dot net core project.   My hope is that I can right click on the project in solution explorer and select "Add new item" and one of the items offered will be Entity Framework.  When I select it, I hope to then get the wizard.  

    It appears that everything has changed, and the websites that indicate how to set things up are not complete, as after many hours I am not able to duplicate the same effort that used to be accomplished with "right Click".

    Is that really true, that the wizard has been removed?

    • Edited by danielsn Tuesday, December 25, 2018 10:22 PM
    Tuesday, December 25, 2018 8:48 PM

Answers

  • I would like to install a database first entity Framework dbcontext on my new dot net core project.   

    If you want to use .NET Core, then you have to use EF Core  DB first.

    The links shows how to do it. There is no more wizard for EF Core like there was for previous versions of EF and there is no more EDMX for EF Core DB first which is dome by scaffolding to create the virtual object model from the database schema.

    https://ef.readthedocs.io/en/staging/platforms/aspnetcore/existing-db.html

    https://www.c-sharpcorner.com/article/getting-started-with-entity-framework-core-database-first-development/

    If you don't want DB first Core, then fine articles on EF Core code first tutorial.

    • Marked as answer by danielsn Tuesday, December 25, 2018 11:39 PM
    Tuesday, December 25, 2018 10:50 PM
  • I think I found the answer here, from someone who ran into the same problem.

    https://stackoverflow.com/questions/43825626/how-to-connect-to-sql-server-from-net-core-without-using-entity-framework

    • Marked as answer by danielsn Wednesday, December 26, 2018 6:10 PM
    Wednesday, December 26, 2018 6:10 PM

All replies

  • I would like to install a database first entity Framework dbcontext on my new dot net core project.   

    If you want to use .NET Core, then you have to use EF Core  DB first.

    The links shows how to do it. There is no more wizard for EF Core like there was for previous versions of EF and there is no more EDMX for EF Core DB first which is dome by scaffolding to create the virtual object model from the database schema.

    https://ef.readthedocs.io/en/staging/platforms/aspnetcore/existing-db.html

    https://www.c-sharpcorner.com/article/getting-started-with-entity-framework-core-database-first-development/

    If you don't want DB first Core, then fine articles on EF Core code first tutorial.

    • Marked as answer by danielsn Tuesday, December 25, 2018 11:39 PM
    Tuesday, December 25, 2018 10:50 PM
  • Thank you.  

    I am going to create another project as a windows library and include it in my project, and do database first from there.  If that doesn't work then I will use ADO.Net or something.

    Although there is claimed to be a tiny advantage in code first, I am not interested in spending the many hours/days required in order to gain that tiny (theoretical) advantage. 

    The previous contract I was on used code first and they had some issue where every query would bring back the entire database's data.  So, their solution was to write exclusions with the query so as to not get back the entire database on each query. 

    Compare their many days of effort as a team to get things to work with code first, until they finally gave up and included commands to exclude data so they wouldn't get the entire database, versus "right click" in terms of effort, and the correct choice is obvious to me.

    Someday when I have nothing else to do, and the weather is rainy, and I can't afford a movie, and well, I guess I will probably never use code first, as life will never be that bleak.

    thanks for the information so I can quit searching, 

    Tuesday, December 25, 2018 11:55 PM
  • I am going to create another project as a windows library and include it in my project, and do database first from there.  If that doesn't work then I will use ADO.Net or something.

    Core of any kind does not work with Windows anything. You have a Core classlib project and you have Core ASP.NET. Core will not be working with Windows desktop solutions until Core version 3.

    Everything in Core is using services and dependency injection. So beware of it. 

    Wednesday, December 26, 2018 1:30 AM
  • What I actually like about Core is the cool stuff for Razor pages using MVVM.  Is there any way to get that functionality without having to use Entity code first to access the azure db?

    I do understand that real men don't use wizards,... and this is the real reason for getting rid of DB first wizard. 


    • Edited by danielsn Wednesday, December 26, 2018 4:31 AM
    Wednesday, December 26, 2018 4:31 AM
  • What I actually like about Core is the cool stuff for Razor pages using MVVM.  Is there any way to get that functionality without having to use Entity code first to access the azure db?

    I do understand that real men don't use wizards,... and this is the real reason for getting rid of DB first wizard. 


    I don't see where using EF Core code first or DB first makes a difference on which approach you choose  One tends to use code first for a non existing database while using DB first is for an existing database. But developers don't follow that strictly. 

    But after that in using C# or VB.NET and using EF against a database,  EF is EF as far as using Linq and other aspects of using EF's functionality  for data persistence and programming against the virtual object model.

    So the only examples you maybe seeing are code first examples,  but that doesn't mean that you cannot use DB first in making the virtual object model along with making the adjustments to not use the virtual model in the way that code first uses the virtual object model and what you see in coding using code first.

    And besides, you can use SoC and segregate functionality.

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

    You can use an architectural patterns like layered or n-tier.

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

    You can use the repository and/or DAO pattern. I don't recommend using the generic repository pattern.

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

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

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

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

    You can also use the DTO pattern.

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

    I also don't confuse the model with the ORM's persistence model, which I see a lot of MVC EF code first examples do.

    https://blog.sapiensworks.com/post/2012/04/07/Just-Stop-It!-The-Domain-Model-Is-Not-The-Persistence-Model.aspx

    If using the repository or DAO pattern, then what difference does it make if the virtual object model was created using EF code first or DB first as I see it?

    EF Core example using Core classlib project called the DAL with the DAL using the DAO and DTO pattern. The DTO is sent through the layers and each layer has project reference to Entities or call whatever you want to call it, a classlib project, where the DTO(s) are kept.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Entities;
    
    namespace DAL
    {
        public interface IDaoProject
        {
            DtoProject GetProjectById(int id);
            List<DtoProject> GetProjectsByUserId(string userid);
            void CreateProject(DtoProject dto);
            void UpdateProject(DtoProject dto);
            void DeleteProject(int id);
        }
    }
    
    ================================
    
    using System.Collections.Generic;
    using System.Linq;
    using System.Transactions;
    using DAL.Models.DB;
    using Entities;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Options;
    
    namespace DAL
    {
        public class DaoProject :IDaoProject
        {
            private readonly IOptions<ConnectionStrings> _options;
            
            public DaoProject(IOptions<ConnectionStrings> options)
            {
                _options = options;
            }
    
            public DtoProject GetProjectById(int id)
            {
                var dto = new DtoProject();
    
                using (var context = new ProjectManagementContext(_options))
                {
                    var project = (context.Projects.Where(a => a.ProjectId == id)).SingleOrDefault();
    
                    if (project == null) return dto;
                    dto.ProjectId = project.ProjectId;
                    dto.ClientName = project.ClientName;
                    dto.ProjectName = project.ProjectName;
                    dto.Technology = project.Technology;
                    dto.ProjectType = project.ProjectType;
                    dto.UserId = project.UserId;
                    dto.StartDate = project.StartDate;
                    dto.EndDate = project.EndDate;
                    dto.Cost = project.Cost;
                }
    
                return dto;
            }
    
            public List<DtoProject> GetProjectsByUserId(string userid)
            {
                var dtos = new List<DtoProject>();
    
                using (var context = new ProjectManagementContext(_options))
                {
                    
                    dtos = (from a in context.Projects.Where(a => a.UserId.Contains(userid))
                        select new DtoProject
                        {
                            ProjectId = a.ProjectId,
                            ClientName = a.ClientName,
                            ProjectName = a.ProjectName,
                            Technology = a.Technology,
                            ProjectType = a.ProjectType,
                            UserId = a.UserId,
                            StartDate = a.StartDate,
                            EndDate = a.EndDate,
                            Cost = a.Cost
                        }).ToList();
                }
    
                return dtos;
            }
    
            public void CreateProject(DtoProject dto)
            {
                using (var context = new ProjectManagementContext(_options))
                {
                    var project = new Projects
                    {
                        ClientName = dto.ClientName,
                        ProjectName = dto.ProjectName,
                        Technology = dto.Technology,
                        ProjectType = dto.ProjectType,
                        UserId = dto.UserId,
                        StartDate = dto.StartDate,
                        EndDate = dto.EndDate,
                        Cost = dto.Cost
                    };
    
                    context.Projects.Add(project);
                    context.SaveChanges();
               }
            }
    
            public void UpdateProject(DtoProject dto)
            {
                var project = new Projects();
                
                using (var context = new ProjectManagementContext(_options))
                {
                   project = (context.Projects.Where(a => a.ProjectId == dto.ProjectId)).SingleOrDefault();
                }
    
                if (project != null)
                {
                    project.ClientName = dto.ClientName;
                    project.ProjectName = dto.ProjectName;
                    project.Technology = dto.Technology;
                    project.ProjectType = dto.ProjectType;
                    project.UserId = dto.UserId;
                    project.StartDate = dto.StartDate;
                    project.EndDate = dto.EndDate;
                    project.Cost = dto.Cost;
                }
    
                using (var dbcontext = new ProjectManagementContext(_options))
                {
                    if (project == null) return;
                    dbcontext.Entry(project).State = EntityState.Modified;
                    dbcontext.SaveChanges();
                }
            }
    
            public void DeleteProject(int id)
            {
                Projects project;
    
                using (var context = new ProjectManagementContext(_options))
                {
                   project = (context.Projects.Where(a => a.ProjectId == id)).SingleOrDefault();
                }
    
                if (project == null) return;
    
                using (var newContext = new ProjectManagementContext(_options))
                {
                   
                    var tasks = new DaoTask(_options).GetTasksByProjectId(project.ProjectId);
                    using (TransactionScope scope = new TransactionScope())
                    {
                        foreach (var task in tasks)
                        {
                            new DaoTask(_options).DeleteTask(task.TaskId);
                        }
    
                        newContext.Entry(project).State = EntityState.Deleted;
                        newContext.SaveChanges();
    
                        scope.Complete();
                    }
                }
            }
        }
    }
    

    using System;
    
    namespace Entities
    {
        public class DtoProject
        {
            public int ProjectId { get; set; }
            public string ClientName { get; set; }
            public string ProjectName { get; set; }
            public string Technology { get; set; }
            public string ProjectType { get; set; }
            public string UserId { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public decimal Cost { get; set; }
        }
    }
    

     
    Wednesday, December 26, 2018 7:50 AM
  • Thank you for your time and your help on this.  Since you asked, I will try to explain why it matters to use the wizard or not.

    Studies have found that having one new tech in a project is okay, but having two new technologies increases its risk of failure to more than 50%. And it is true.  

    The time savings is also a big deal when working at home, as is the decrease in complexity.  Even though it may be hundreds of lines of code, or even thousands, it is just one simple thing, easy to manage, easy to understand. And especially easy to maintain.  Just click and select refresh, then go and generate scripts for the db and done.  The whole thing takes five minutes and when I am done, I know it is right. 

    So, it is not just about generating it.  it is about all these things, and probably the management of complexity is the most important.  In order to work fast at home, complexity has to be kept as low as possible.  

    There are additional reasons, but these are the main ones.  (remember, I already spent four hours and accomplished NOTHING, with advice for my particular error code being scarce, but things like "reinstall VS were suggested.  This compared to right click, boom, done.)

    I just got book from Apress on Entity Framework Core 2.0 which is 300 plus pages.  Guess I will start reading, and eventually I will be able to accomplish everything I was once able to accomplish with a right click.

    Once I have finished reading these 329 pages, in order to discover the 50 pages I need, I will implement a database first, and after that probably not use it again for at least a year. At which time, I will likely not remember anything from those 329 pages, and only a small amount from the 50 pages and have to all but start over.  All to replace what was once easily doable with a right click.







    • Edited by danielsn Wednesday, December 26, 2018 5:10 PM
    Wednesday, December 26, 2018 3:49 PM
  • I think I found the answer here, from someone who ran into the same problem.

    https://stackoverflow.com/questions/43825626/how-to-connect-to-sql-server-from-net-core-without-using-entity-framework

    • Marked as answer by danielsn Wednesday, December 26, 2018 6:10 PM
    Wednesday, December 26, 2018 6:10 PM
  • I think I found the answer here, from someone who ran into the same problem.

    https://stackoverflow.com/questions/43825626/how-to-connect-to-sql-server-from-net-core-without-using-entity-framework


    Sure one can use straight up ADO.NET, SQL Command Objects, parmterized inline T-SQL or parmterized stored procedure and a datareader with using a datatable or the DTO pattern for CRUD with the database. Nothing has changed there between using Core or non Core.
    Wednesday, December 26, 2018 8:33 PM
  • I am confused on one thing.  

    I had a project reference to my Windows class library, which includes my use of Entity Framework 6.2.  Just to see what it did I tried accessing one of the classes from my Core 2.2 project, and it seemed to work fine.

    Can you please explain what you meant about it not working?  I understand it will break the ability of me to run it on a Linux box, but other than that, it seems to be working fine.

    What is the problem you were referring to that made you say that it wouldn't work?

    Thursday, December 27, 2018 3:29 PM
  • That I know about and I have seen in use, one doesn't mix .NET Core with a solution like EF 6 that is non Core. Now you may be able to get away with what you are doing,  but it might comeback and take big wet juicy bite out of you at some point. :)
    Thursday, December 27, 2018 10:10 PM
  • found a blurb about it.

    https://www.ben-morris.com/sharing-libraries-between-net-core-and-net-framework-applications/

    Apparently as long as I used the right library and the right tools along with it, I am good.


    Thursday, December 27, 2018 11:21 PM
  • found a blurb about it.

    https://www.ben-morris.com/sharing-libraries-between-net-core-and-net-framework-applications/

    Apparently as long as I used the right library and the right tools along with it, I am good.



    I suspect that EF 6 will just become a legacy solution,  and no new improvements will be done with legacy EF 6. 
    Friday, December 28, 2018 12:51 AM