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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:28:15+00:00 2026-05-29T06:28:15+00:00

Just a little idea I’m playing with, not sure if it’s viable or has

  • 0

Just a little idea I’m playing with, not sure if it’s viable or has much of a use.

I’m trying to generate a very basic EF Code First database using the Roslyn CTP.

Code:

var scriptEngine = new ScriptEngine(new[] { "System", "System.Core", typeof(DbContext).Assembly.Location });
var session = Roslyn.Scripting.Session.Create();

var t = scriptEngine.CompileSubmission<DbContext>(@"
  using System.Data.Entity;         
  public class Car  {
    public int Id {get; set;}
    public string Name {get;  set; }
  }

  public class Context : DbContext {
    public DbSet<Car> Cars {get; set; }
  }

  new Context();
", session);

t.Execute();

When executed I get the following exception

Exception:

The type ‘Submission#0+Car’ was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.

Looking through the list of possible issues, I’m guessing that Roslyn is making a nested class as part of the code gen. This makes sense otherwise the “new Context();” call would need to be wrapped into a class/method of some sort. I could emit an assembly, which would confirm the above but likely wouldn’t have any clues on how to write it correctly.

I also went down the route of Syntax.ClassDeclaration, but ended up with a few hundred lines of code just to make a class with 1 property and no obvious way how to instantiate that class.

Question

Is there an easy way to create a class in Roslyn that is publicly accessible (eg not nested in another class)?

  • 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-29T06:28:16+00:00Added an answer on May 29, 2026 at 6:28 am

    You can use Roslyn to create actual DLL library that contains your type based on your source code and then use that from your script:

    var classCode = @"
    using System.Data.Entity;   
    
    public class Car  {
        public int Id { get; set; }
        public string Name { get;  set; }
    }
    
    public class Context : DbContext {
        public DbSet<Car> Cars { get; set; }
    }";
    
    var syntaxTree = SyntaxTree.ParseCompilationUnit(classCode);
    
    var compilation = Compilation.Create(
        "car",
        new CompilationOptions(assemblyKind: AssemblyKind.DynamicallyLinkedLibrary))
        .AddReferences(
            new AssemblyFileReference(typeof(object).Assembly.Location), // mscorlib
            new AssemblyFileReference(typeof(Uri).Assembly.Location), // System
            new AssemblyFileReference(typeof(IOrderedQueryable<>).Assembly.Location), // System.Data
            new AssemblyFileReference(typeof(DbContext).Assembly.Location) // EntityFramework
        )
        .AddSyntaxTrees(syntaxTree);
    
    var dllPath = "car.dll";
    using (var stream = File.OpenWrite(dllPath))
    {
        compilation.Emit(stream);
    }
    
    var code = @"new Context();";
    var scriptEngine = new ScriptEngine(new[] { new FileInfo(dllPath).FullName, "EntityFramework" });
    
    var context = scriptEngine.Execute<DbContext>(code);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a little question for you... I'm currently trying to implement a role structure
I am sure it's just a little mistake, but i can't find it. When
I have very little idea what I'm doing here, I've never done anything like
Same idea as my previous question , just a little more advanced. I want
I'm trying to build (right now just thinking/planning/drawing relations :] ) little modular system
I recently began developing a little javascript game, just for fun. The idea was
My whole idea is a little more complex, but just to break things down
Firstly, I'm by no means a web designer so I have very little idea
Just looking for a little feedback on if this is a bad idea for
So I've just started playing around with OpenGL, and decided to make a little

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.