locked
use Entity Framework RRS feed

  • Question

  • User-366177243 posted

    Hi, 

    I want to start using the entity framework. 
    I've seen several videos but each one works differently.

    Can you tell me how to work with the entity framework? what do you need to install?

    Thank you all

    Monday, August 24, 2020 10:39 AM

Answers

  • User1686398519 posted

    Hi klasss,

    Entity Framework is a set of technologies in ADO.NET. It is not just another object-relational mapping solution, it also enables applications to access and change data represented as entities and relationships in the conceptual model.

    There are three ways to use Entity Framework 6 in ASP.NET MVC: "Code First", "Database First" and "Model First". For information on how to choose between "Code First", "Database First" and "Model First", see "Creating Models."Below I will briefly introduce Entity Framework 6 Code First in ASP.NET MVC 

    Entity Framework 6 Code First:The simple explanation is that you create the model first, and then update the database through code-first migration.

    1. You can use the scaffolding to automatically install the EF NuGet package while creating the database context class and creating the connection string.                                                                                                                                                                                                                
    2. Create a Model  and Code First Migrations
      • Enable-Migrations
      • Add-Migration
      • Update-Database

    The following is a simple example, please refer to it.

    Model

        public class Test
        {
            public int id { get; set; }
            public string name { get; set; }
        }

    TestContext

        public class TestContext : DbContext
        {
            public TestContext()
                : base("name=TestContext")
            {
            }
            public virtual DbSet<Test> Tests { get; set; }
        }

    Controller

            public TestContext db = new TestContext();
            public ActionResult Index()
            {
                var list = db.Tests.ToList();
                return View(list);
            }

    Index

    @model  IEnumerable<WebApplication5.Models.Test>
    @foreach(var item in Model)
    {
        @Html.DisplayTextFor(m=>item.name)
    }

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 25, 2020 2:52 AM

All replies

  • User753101303 posted

    Hi,

    You are using ASP.NET 4.x or ASP.NET Core? Start by installing the nuget package if not done already:
    https://docs.microsoft.com/en-us/ef/ef6/fundamentals/install

    You can either create the database from EF C# code or create EF C# code from an existing database which is perhaps the difference you saw?

    If you don"t want yet some other stuff that perhaps won't help try to be explicit about the point on which you are stuck. For now I assume the very beginning ie not even having EF available inside your app?

    Monday, August 24, 2020 11:13 AM
  • User475983607 posted

    klasss

    Can you tell me how to work with the entity framework? what do you need to install?

    Entity Frame work was initially release in 2008 as an API within the .NET framework.  Today the latest version is EF 6 and EF Core are separate packages installed through NuGet.  Nuget is a package manager integrated within Visual Studio.

    What version if .NET are you targeting?  This information will help us help you get the correct version of EF.

    https://docs.microsoft.com/en-us/ef/ef6/get-started

    https://docs.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli

    Monday, August 24, 2020 11:49 AM
  • User1686398519 posted

    Hi klasss,

    Entity Framework is a set of technologies in ADO.NET. It is not just another object-relational mapping solution, it also enables applications to access and change data represented as entities and relationships in the conceptual model.

    There are three ways to use Entity Framework 6 in ASP.NET MVC: "Code First", "Database First" and "Model First". For information on how to choose between "Code First", "Database First" and "Model First", see "Creating Models."Below I will briefly introduce Entity Framework 6 Code First in ASP.NET MVC 

    Entity Framework 6 Code First:The simple explanation is that you create the model first, and then update the database through code-first migration.

    1. You can use the scaffolding to automatically install the EF NuGet package while creating the database context class and creating the connection string.                                                                                                                                                                                                                
    2. Create a Model  and Code First Migrations
      • Enable-Migrations
      • Add-Migration
      • Update-Database

    The following is a simple example, please refer to it.

    Model

        public class Test
        {
            public int id { get; set; }
            public string name { get; set; }
        }

    TestContext

        public class TestContext : DbContext
        {
            public TestContext()
                : base("name=TestContext")
            {
            }
            public virtual DbSet<Test> Tests { get; set; }
        }

    Controller

            public TestContext db = new TestContext();
            public ActionResult Index()
            {
                var list = db.Tests.ToList();
                return View(list);
            }

    Index

    @model  IEnumerable<WebApplication5.Models.Test>
    @foreach(var item in Model)
    {
        @Html.DisplayTextFor(m=>item.name)
    }

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 25, 2020 2:52 AM