Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework not generating C# files properly (some enums are incomplete, so build fails)

At work I just installed a brand new copy of my OS and a brand new copy of VS2015. When I clone my solution for the first time, I cannot build it anymore, even if I've generated the C# files like I always do, by opening the .edmx file first, and clicking on "save" icon.

When building it throws the error:

CS0150: A constant value is expected

Because the enums that it has generated are incomplete! An example of one:

public enum DocumentType : int
{
    Identity = 1,
    ResidenceProof = 2,
    RegisterDoc = ,
}

I also had this compiler error at the time, but after fixing it my C# enums are still being generated wrongly:

The ADO.NET provider with invariant name 'MySql.Data.MySqlClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details

How the hell should I fix this problem?

like image 417
knocte Avatar asked May 19 '16 11:05

knocte


2 Answers

Not really an answer but i'll post my findings and the workaround i chose to use;

The T4 Code Generation template (The file attached to your .edmx file using the .tt file-extention) contains code to generate C# using the data available in your model, I suspect the code at line #204 (might be located on a different line number in your version) to contain a minor bug.

A screenshot from my active project and solution explorer: Code inside the .tt file

This line causes the faulty enums:

    this.GenerationEnvironment.Remove(this.GenerationEnvironment.Length - 3, 1);

This presumably removes generated code characters that were added by the enum generator in order to remove the last , from the enum, as it is unnecessary.

I tested the functionality of this by adding output in front of this line, i tried creating an enum that would output MyEnumMemberName = TEST, and found that the output contained MyEnumMemberName = TES,.

Using this I found that changing the line to:

    this.GenerationEnvironment.Remove(this.GenerationEnvironment.Length - 2, 1);

solves my issue.

Can't currently check if this also works on the machines that were already generating correct code but I hope it helps some of you. Good luck.

like image 106
Fixation Avatar answered Nov 18 '22 02:11

Fixation


I had the same problem. Turned out that texttransform.exe cannot understand different line endings well. My .tt file was saved with Unix EOL, and when I saved it with Windows EOL, it started to work correctly. Just that simple - open your .tt file in WordPad and save.

like image 7
Alex Butenko Avatar answered Nov 18 '22 00:11

Alex Butenko