locked
DbContext RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below 2 classes and how they will be written in DbContext

    Thanks

     public partial class AuthorViewModel
        {
            [Key]
            public int Id { get; set; }
    
            public string AuthorName { get; set; }
    
            public string Address { get; set; }
        }
    
    
     public partial class AuthorListViewModel
        {
            public IEnumerable<AuthorViewModel> AuthorList {get;set;}
    
        }

    Friday, January 27, 2017 3:45 PM

Answers

  • User281315223 posted

    If you are using Entity Framework, generally your classes will be exposed as DbSet collections under your context. With each "Table" being mapped to a given DbSet, so that you can access it via `_context.Authors.Where(...)` etc.

    public class YourApplicationContext : DbContext
    {
        public YourApplicationContext () : ;
    
        public DbSet<AuthorViewModel> Authors { get; set; }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 27, 2017 9:57 PM
  • User-821857111 posted

    Hi

      I have below 2 classes and how they will be written in DbContext

    Thanks

     public partial class AuthorViewModel
        {
            [Key]
            public int Id { get; set; }
    
            public string AuthorName { get; set; }
    
            public string Address { get; set; }
        }
    
    
     public partial class AuthorListViewModel
        {
            public IEnumerable<AuthorViewModel> AuthorList {get;set;}
    
        }

    ViewModel classes are typically used to represent the data for a view. The classes that you use to work with the DbContext are usually Entity classes that map directly to the underlying database tables. Therefore, your entity class will be Author:

    public class Author
    {
        public int Id {get;set;}
        public string AuthorName {get;set;}
        public string Address {get;set;}
    }

    As Rion said, this will be added to the DbContext as a generically typed DbSet:

    public class MyContext
    {
        pubic DbSet<Author> Authors {get;set;}
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 2, 2017 9:05 AM

All replies

  • User281315223 posted

    If you are using Entity Framework, generally your classes will be exposed as DbSet collections under your context. With each "Table" being mapped to a given DbSet, so that you can access it via `_context.Authors.Where(...)` etc.

    public class YourApplicationContext : DbContext
    {
        public YourApplicationContext () : ;
    
        public DbSet<AuthorViewModel> Authors { get; set; }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 27, 2017 9:57 PM
  • User-1499457942 posted

    Hi Rion

      Do i need to add this also to DbContext

    public partial class AuthorListViewModel
        {
            public IEnumerable<AuthorViewModel> AuthorList {get;set;}
    
        }

    Thanks
    Saturday, January 28, 2017 3:07 AM
  • User-707554951 posted

    Hi JagjitSingh,

      
    using System.Collections.Generic;

    using System.ComponentModel.DataAnnotations;

    using System.Data.Entity;

    public class YourApplicationContext: DbContext { public DbSet<AuthorViewModel> AuthorViewModels { get; set; } public DbSet<AuthorListViewModel> AuthorListViewModels { get; set; } } public partial class AuthorViewModel { [Key] public int Id { get; set; } public string AuthorName { get; set; } public string Address { get; set; } } public partial class AuthorListViewModel { public IEnumerable<AuthorViewModel> AuthorList { get; set; } }

    Best Regards

    Cathy

    Monday, January 30, 2017 8:32 AM
  • User281315223 posted

    JagjitSingh

    Do i need to add this also to DbContext

    You shouldn't need to. The DbSet should be sufficient for accessing your specific AuthorViewModel items. However, it can vary depending on exactly what you are trying to accomplish.

    Monday, January 30, 2017 2:38 PM
  • User-821857111 posted

    Hi

      I have below 2 classes and how they will be written in DbContext

    Thanks

     public partial class AuthorViewModel
        {
            [Key]
            public int Id { get; set; }
    
            public string AuthorName { get; set; }
    
            public string Address { get; set; }
        }
    
    
     public partial class AuthorListViewModel
        {
            public IEnumerable<AuthorViewModel> AuthorList {get;set;}
    
        }

    ViewModel classes are typically used to represent the data for a view. The classes that you use to work with the DbContext are usually Entity classes that map directly to the underlying database tables. Therefore, your entity class will be Author:

    public class Author
    {
        public int Id {get;set;}
        public string AuthorName {get;set;}
        public string Address {get;set;}
    }

    As Rion said, this will be added to the DbContext as a generically typed DbSet:

    public class MyContext
    {
        pubic DbSet<Author> Authors {get;set;}
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 2, 2017 9:05 AM