Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8902309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:34:51+00:00 2026-06-15T01:34:51+00:00

I want to create a model on MVC3, but instead of creating a model

  • 0

I want to create a model on MVC3, but instead of creating a model for a table, I want to create one for a view in my Database. I know that with models for tables, I need to specify primary keys when creating the model, if not i get some error messages. Since views do not have have primary/foreign keys, how do I go about creating models for View?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:34:52+00:00Added an answer on June 15, 2026 at 1:34 am

    At any MVC application that works with EF you may have different models to create tables in db or conceptual model for view that doesn’t create any tables.Table creation depends on what properties of which model classes you’ll define in class that inherited from DbContext. Notice that any property that exists in this class and its relational classes will create table in db. So,to create models that will be used only at view, you can define models that don’t have any relationship to other table creator models plus no definition at inherited DbContext class. Then you can use them as model or class to show data at the view.The controller and its related view should use the same model and no errors will be occurred.

    public class datacontext : DbContext
    {
        public DbSet<Person> people { get; set;}
        public DbSet<Address> address { get; set;}
    }
    public class Person
    {
        [Key]
        public int ID { get; set;}
        public String Name { get; set;}
    }
    public class Address
    {
        [Key]
        public int ID { get; set;}
        public Country country { get; set;}
        public String Details { get; set;}
    }
    public class Country
    {
        [Key]
        public int ID { get; set;}
        public String Name { get; set;}
    }
    public class Work_Field//without any key definition
    {
        public String Title { get; set;}
        public String Description { get; set;}
    }
    

    Here we have this tables : Person, Address, Country(Although it’s not defined at datacontext class)
    In this example, you can use Work_Field as view model perhaps with some DataAnnotations like [Required],… to force user don’t leave parameter empty, and so more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have one model & view that is served by multiple controllers
I want to create one new model class, which will be extended in all
What I want is to create a Model that connects with another using a
This the model that I want to create using json file Ext.define('Users', { extend:
I want to create a graph in Excel with the spreadsheet reference. @model IEnumerable<ARTex.Core.Models.Reservation>
I want to create a model named Model. I'm pretty sure this isn't allowed,
I want to create my Model fields dynamically (in ExtJS 4). For instance, sometimes
I want to create a modal dialog that has more controls than what a
I created model objects of from generating a scaffold, but now I want to
I want to create an application that doesn't use Portrait mode. I am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.