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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:33:42+00:00 2026-06-06T11:33:42+00:00

I’m new to using MVC and I’m trying to make a model that uses

  • 0

I’m new to using MVC and I’m trying to make a model that uses other models.

Say I have in my database 2 tables, i’ll just call them Table1 and Table2 for the sake of this question. In my MVC project these correspond to Model1 and Model2. Now I’d like to have Model 3 that’s like:

public class Model3
{
    public Model1 model1 { get; set; }
    public Model2 model2 { get; set; }
    public string someString { get; set; }
}

Which I want to create with a T-SQL query that looks like this:

SELECT * FROM Table1 t1
LEFT JOIN Table2 t2
    ON t1.fk = t2.pk

Now, to run this query i’m using the technique described at http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/advanced-entity-framework-scenarios-for-an-mvc-web-application

The relevant code is:

public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters)
{
    return dbSet.SqlQuery(query, parameters).ToList();
}

where I feed the above query in.

The problem (finally!) is that Entity Framework wants a key. This wouldn’t be a problem but in this case, the key is Table1.table1KeyPart and Table2.table2KeyPart, corresponding to Model1.model1Keypart and Model2.model2keypart.

So I tried using the second technique here:
in entity framework code first, how to use KeyAttribute on multiple columns

That is, the one using HasKey, with u.Model1.model1KeyPart, u.Model2.model2KeyPart instead of userID and userName. Like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Model3>().HasKey(u => new
        {
            u.Model1.model1KeyPart,
            u.Model2.model2keyPart
        });
    }

I get the error:

The properties expression ‘[u => new <>f__AnonymousType1`2(model1KeyPart = u.Model1.model1KeyPart, model2KeyPart = u.Model2.model2KeyPart)]’ is
not valid. The expression should represent a property: C#: ‘t =>
t.MyProperty’ VB.Net: ‘Function(t) t.MyProperty’. When specifying
multiple properties use an anonymous type: C#: ‘t => new {
t.MyProperty1, t.MyProperty2 }’ VB.Net: ‘Function(t) New From {
t.MyProperty1, t.MyProperty2 }’.

Is this something that’s fixable? Or am I doing this the complete wrong way? I suppose I could just list out all the members of Model1 and Model2 in Model3, but that’s introducing a redundancy that I really don’t want, but if it’s the only way, then I’ll do it.

Sorry if any of that was unclear, I’ll try to clear things up if need be. Thanks in advance for any help!

  • 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-06T11:33:45+00:00Added an answer on June 6, 2026 at 11:33 am

    I think you need to define the composite key, if you are using Entity Framework code first you can do this in dbContext.cs in OnModelCreating method like this

    modelBuilder.Entity<Model3>().HasKey(s => new { s.Model1Key, s.Model2Key });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a reasonable size flat file database of text documents mostly saved in
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.