My question might be little bit different or basic for advanced users here.
I have a web application which is customizable by administrators. This means, the admin can add new table schema or edit table columns, add new table columns etc. These changes are mapped to our logical objects (much similar to EDMX) which is published after the change. While publishing we generate Stored procedures with necessary changes that are required.
From UI we use these Logical objects to connect DB (using COM which understands the mapping and executes appropriate sprocs and views etc). Now i am thinking to use EF for repl开发者_JAVA技巧acing the Logical objects model which we are having currently. I can create the EDMX files (csdl,msl, ssdl, cs files) dynamically but i am not sure how to compile them and package the classes into DLL dynamically. This means, when i click the button the all edmx related files will be created and DLLs must be created based on CS files and the website must be able to access the new changes in the code.
Can you help me how to automatically and dynamically compile the cs files. I will not have the source code of other files (like default.aspx and others) at customer's end.
Thanks Albert
Bepenfriends - rather than attempting a CSC from a shell, consider using a CSharpCodeProvider.CompileAssemblyFromFile.
One thing to be careful of: if you compile code and then try to compile it again, you may lock up on the assembly that was created. For this I created an AsyncCompiler which creates a new AppDomain, compiles in there, and then unloads the AppDomain.
It's some pretty hairy code but I'm doing this (generating the AEF XML files, generating code from them, and then compiling everything into an assembly) and it works well. The only thing I don't have quite right is dynamically importing stored procedures - my SSDL has them but not the CSDL or MSL. The search for a solution led me here.
I hope the keywords here are enough to set you off in the right direction.
I don't really have a definitive answer for you just yet - but two potential solutions you could look into, possibly in the near future:
1) There's a set of CodeSmith templates called PLINQO which generate your Linq-to-SQL model (DBMX) and its associated *.cs files from your database. I asked them about Entity Framework support myself, and they confirmed to be working on it - so maybe they'll have that some time soon.
Since those are code and model generation templates, you could definitely take those for Linq-to-SQL and tweak them for EDMX, too. A bit of work, but definitely possible. Once you have the *.cs files and the *.edmx model, you can generate a resulting assembly from it using the CSC (C#) command-line compiler that's installed on every machine which has .NET installed.
2) With .NET 4, the new Entity Framework 4 will include T4 templates (another code generation technology) which will allow you to customize your code generation. Same story applies here - you could externally generate your EDMX model and associated *.cs files for the classes and generate an assembly on the fly from those templates.
See another blog post on that topic, and you should find lots of information when gooling (or binging) or "EF4 T4 templates".
Hope that helps at least a little bit!
精彩评论