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

  • Home
  • SEARCH
  • 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 4017516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:52:14+00:00 2026-05-20T09:52:14+00:00

I am creating a linq-to-sql model with EF, and have a class that works

  • 0

I am creating a linq-to-sql model with EF, and have a class that works well, however, I want to be able to make additions to the class and not have them overwritten when I make changes with EF, so I believe I need to use a second layer of view class.

So, I have:

public partial class People: INotifyPropertyChanging, INotifyPropertyChanged {...} // created by EF

then want to do:

public partial class ViewPeople: People {
   public String someFunction() {...} // additional functionality
}

however, when I try to cast from People to ViewPeople using the code further below, I get an exception:

Unable to cast object of type ‘Namespace.Models.People’ to ‘Namespace.Models.ViewPeople’

Code:

//select the person
NSDataContext dc = new NSDataContext(); // to get to the data context and models froM EF
var person = (from p in dc.Peoples where p.id == personID select p).First();

// pass model to view
return View((ViewPeople)person)

If I just pass through person, I get access to all the “People” properties and methods.
When I modify the ViewPeople class, i get access to all properties and methods of the super “People” class when referencing ‘this’.

Sure it’s something simple, but I just can’t figure it out!

Any assistance on the matter would be appreciated, as would an example to where someone has implemented viewmodels ontop of auto-generated EF models!

Thanks in advance,
Andrew

  • 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-05-20T09:52:15+00:00Added an answer on May 20, 2026 at 9:52 am

    When the entity framework returns objects from data it fetches from the database, it creates People, not ViewPeople. That explains why you cannot cast your objects. If you want to add functionality to your People class, you can add this in another file in the same assembly:

    public partial class People
    {
        public String someFunction() {...} // additional functionality
    }
    

    Another thing you can do is make the ViewPeople class take a People instance in its constructor, wrap the People instance’s properties (yikes!) and change your query to be:

    //select the person
    NSDataContext dc = new NSDataContext(); // to get to the data context and models froM EF
    ViewPeople person = (from p in dc.Peoples where p.id == personID select new ViewPeople(p)).First();
    
    // pass model to view
    return View(person)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using LINQ to Entities (not LINQ to SQL) and I'm having trouble creating
When creating a LINQ to SQL class (dbml) in an App_Code folder for a
Preamble: I'm creating some tables in SQL Server that will be accessed via Linq-to-entities
I am creating serialized XML for a LINQ to SQL project using the DataContractSerializer
I am new to LINQ to SQL, but have done a lot of database
So, I have an app that uses a SQL Server express db. I have
Am creating a table with sql server 2008 and using linq to sql. Am
I am creating entries in a contract table using linq to sql. One column
I would like to be able to subclass automatically generated LINQ to SQL Data
When creating a web application, and lets say you have a User object denoting

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.