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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:11:15+00:00 2026-06-17T09:11:15+00:00

I’m working with Entity Framework, trying to test code that hits it. All the

  • 0

I’m working with Entity Framework, trying to test code that hits it. All the code runs fine, but I’m having trouble with my testing. When I run this code when testing:

_entities = new Mock<MyEntities>();
Database.SetInitializer(new DropCreateDatabaseAlways<MyEntities>());
_entities.Object.Database.Initialize(true);

I get the “EntityType has no key defined” error. I was getting these before because some of the tables don’t follow the convention of naming their id columns “ID” or “[Table Name]ID.” After cleaning those up, I still get them for tables with a composite key. They’re defined correctly as far as SQL is concerned. In the EDMX designer, when I click on the columns, they say they’re keys. So how do I properly define them as keys? Where do I put the code?

I tried looking this up and ALL references are specifically to Code First implementations. I tried it anyway. They said to override the OnModelCreating method of the DbContext object and add this to it:

modelBuilder.Entity<Widgets>().HasKey(widget => new { widget.A, widget.B });

So I made a partial class and tried to override it. It was already overriden. So I went to the generated partial class. Here’s the implementation:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}

So that’s not right. So what do I do now?

I want to generate a test database based off of the EDMX in my web project so I can create proper integration tests. Thanks in advance.

  • 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-17T09:11:16+00:00Added an answer on June 17, 2026 at 9:11 am

    If you are using EDMX, you can’t use databsae initialization (or onmodelcreating). Those features are specific to Code First.

    So there’s really no answer here because you can’t generate an test database from an EDMX.

    There are some patterns you can follow for creating a test database on the fly. Pastingi from a blog post I wrote in 2009 (http://thedatafarm.com/blog/data-access/unit-testing-in-ef-v1-the-database-conundrum/)

    • Copy/Paste Database: Use a small SQL Server Express database as the
      testing database. In the course of the particular test, copy/paste
      the db file to a specific location and then open a connection to it.
      At the end of the test, blow away the file.

    • Transactions: In each of the db access tests, wrap the code within a
      System.Transaction.TransactionScope. At the end of the test, don’t
      complete the transaction and the db will roll back.

    • DB Script:
      Another client was using a small database that he would generate on
      the fly at the start of each test and then remove the db at the end.

    So given that, the place to define your key is (as I suggested on twitter) in your model. In the designer, right click on each entity property that belongs in the key and make sure “entity key” is clicked on the context menu. EF will then use the combination of those properties as it’s composite entity key.

    Hope it helps.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
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'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.