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 6862175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:37:45+00:00 2026-05-27T02:37:45+00:00

I am trying to create a custom Entity Framework (4.2) entity that would be

  • 0

I am trying to create a custom Entity Framework (4.2) entity that would be mapped to my database like it would be done in a Code first approach.

The issue is that my entity framework data model is using Database first.

How can I add my custom entity to entity framework’s context?

  • 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-27T02:37:46+00:00Added an answer on May 27, 2026 at 2:37 am

    If by the Database first you mean that you already have EDMX created from exiting database you simply cannot use code first. You must create table and update model (EDMX) from the database to include it in EDMX.

    Edit based on comment:

    I want to create a BriefUser entity that would basically be a lighter
    version of User but it would have properties retrieved from User’s
    foreign keys.

    Well this is possible. You can either create BriefUser as common class and use projection in query.

    var breifUser = (from x in context.Users
                     where ...
                     select new BriefUser
                     {
                         // Fill BreifUser's properties here
                     }).FirstOrDefault();
    

    You can even refactor former code to reusable extension method:

    public static IQueryable<BriefUser> ProjectUser(this IQueryable<User> query)
    {
        return query.Select(x => new BreifUser() 
                                 { // Fill BreifUser's properties here });
    }
    

    and use it like:

    var briefUser = context.Users.ProjectUser().FirstOrDefault(...);
    

    It is also possible to define your new class as “entity view”. The first problem is that each table can be mapped to only one entity (except some advanced concepts like inheritance or splitting) so you cannot define your BriefUser as a new entity type because mapping both User and BriefUser to UserTbl would violate this rule. You must use special construct called QueryView.

    QueryView is view in mapping level. It allows you to create new mapped type which is projection of existing mapped entities defined directly in EDMX’s MSL part. The projection is defined as custom Entity SQL query. The problem is that QueryView has limitations:

    • It doesn’t offer all Entity SQL features – for example it doesn’t support aggregations (which I consider as really missing feature). Without aggregations you for example cannot create a new type which will contain property counting some related entities.
    • It is not supported in designer. You must edit your EDMX as XML to define QueryView and you must write Entity SQL query yourselves.
    • Resulting type is a “view” and it is read-only.

    I want to keep the EDMX file, but also be able to add an entity
    (BriefUser) to EF’s context.

    This is not possible. Your BreifUser is only projection / view and EF is not able to track changes back to originating tables so you cannot add BreifUser to context and persist it. In case of QueryView you can achieve it if you define custom stored procedures which will no how to decompose BreifUser and modify all related tables. These stored procedures must be imported to the EDMX and mapped to data modification operations of the view entity. Btw. same will happen if you map your entity to the database view because EF takes all views as read-only.

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

Sidebar

Related Questions

I'm trying to create a custom JSP tag that would take an array object
I'm trying to create a custom UIButton that should look like a UIButtonTypeRoundedRect. In
I'm trying to create custom view that draws image downloaded from Url. The code
I am having a trouble while trying to create an entity with a custom
I am trying to create a custom accordion for my page to that display
I'm trying to create a custom MembershipProvider and I was wondering how I would
Trying to create a custom component that gets it's layout from an XML file
I'm using ASP.NET MVC 3 and Entity Framework 4.1 Code First. Let's say I
I have an ASPNET MVC 3 project made with Entity Framework 4 (database first).
Im trying to create a custom version of the RequiredAttribute to replace the built

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.