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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:02:21+00:00 2026-05-21T02:02:21+00:00

I hate doing this but I’ve spent way too much time on this. The

  • 0

I hate doing this but I’ve spent way too much time on this. The following is an attempt to convert the c# method to a MethodBuilder. The Result should be the override method for the EntityFramework OnModelCreating using EFCodeFirst.

Here is the method in c# (model names shortened for simplicity)

protected override void OnModelCreating(ModelBuilder modelBuilder) {
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    modelBuilder.Entity<Form>().ToTable("Form<Name>");
    modelBuilder.Entity<FormAg>().ToTable("FormAg<Name>");
}

Here’s the MethodBuilder I’m currently using:

private static MethodBuilder FormContextBuildMethodOnModelCreating(TypeBuilder type, string formId) {
    System.Reflection.MethodAttributes methodAttributes = System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Family | System.Reflection.MethodAttributes.Virtual | System.Reflection.MethodAttributes.HideBySig;

    //method
    MethodBuilder OnModelCreating = type.DefineMethod("OnModelCreating", methodAttributes);

    //method calls
    MethodInfo Conventions = typeof(ModelBuilder).GetMethod("get_Conventions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { }, null);
    MethodInfo RemoveConventions = typeof(ConventionsConfiguration).GetMethod("Remove", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { }, null);

    MethodInfo Entity = typeof(ModelBuilder).GetMethod("Entity", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { }, null);
    MethodInfo ToTable1 = typeof(System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<>).MakeGenericType(typeof(Form)).GetMethod("ToTable", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(String) }, null);
    MethodInfo ToTable2 = typeof(System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<>).MakeGenericType(typeof(FormAg)).GetMethod("ToTable", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(String) }, null);

    OnModelCreating.SetReturnType(typeof(void));
    OnModelCreating.SetParameters(typeof(ModelBuilder));

    ParameterBuilder modelBuilder = OnModelCreating.DefineParameter(1, ParameterAttributes.None, "modelBuilder");
    ILGenerator Il = OnModelCreating.GetILGenerator();

    Il.Emit(OpCodes.Nop);
    Il.Emit(OpCodes.Ldarg_1);
    Il.Emit(OpCodes.Callvirt, Conventions);
    Il.Emit(OpCodes.Callvirt, RemoveConventions);
    Il.Emit(OpCodes.Nop);
    Il.Emit(OpCodes.Ldarg_1);
    Il.Emit(OpCodes.Callvirt, Entity);
    Il.Emit(OpCodes.Ldstr, "Form" + formId);
    Il.Emit(OpCodes.Callvirt, ToTable1);
    Il.Emit(OpCodes.Nop);
    Il.Emit(OpCodes.Ldarg_1);
    Il.Emit(OpCodes.Callvirt, Entity);
    Il.Emit(OpCodes.Ldstr, "FormAg" + formId);
    Il.Emit(OpCodes.Callvirt, ToTable2);
    Il.Emit(OpCodes.Nop);
    Il.Emit(OpCodes.Ret);

    return OnModelCreating;
}

The OpCodes were gleaned from reflector il of the above c# code. For the life of me I can not figure out what is wrong here. I’ve narrowed the issue down to this method as all the other properties and what not are built correctly. I can return an empty body (Nop + Ret) and it works fine…

Any attempt to use the resulting type causes the following error:
An attempt was made to load a program with an incorrect format.

Any help would be greatly appreciated.

  • 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-21T02:02:22+00:00Added an answer on May 21, 2026 at 2:02 am

    Just gonna answer this question to close it.

    I created another class that inherits from the base class that contains the OnModelCreating with a new constructor that passes in the formId.

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

Sidebar

Related Questions

I hate starting a post with this but I'm new to Java... I've followed
I hate case sensitivity in databases, but I'm developing for a client who uses
I hate Physics, but I love software development. When I go back to school
I hate to have to ask, but I'm pretty stuck here. I need to
For example, I hate typing things like: $x = mysql_escape_string(stripslashes($_GET['x'])); Is there a way
I hate having a bunch of left/right methods. Every time a property is added
I'd like to embed a google calendar in my website, but I hate the
I hate this stuff. Just to note. + means OR * means AND !
I know it probably doesnt matter/affect performance for the most part but I hate
I hate asking for code but I just can't seem to do the below

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.