11

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?

knocte
  • 16,941
  • 11
  • 79
  • 125
  • What message is contained in the `InnerException`? – David Tansey May 20 '16 at 02:32
  • The following post might be helpful:http://stackoverflow.com/questions/15142841/no-entity-framework-provider-found-for-mysql-data-mysqlclient-ado-net-provider -- but I'm not certain. – David Tansey May 20 '16 at 02:35
  • @DavidTansey: not sure this is the same problem, I don't have exceptions but compiler errors – knocte May 20 '16 at 02:41
  • Yeah, I understand what you're saying. I only asked because of the part of the message that says: _See the inner exception for details_ I would at least compare the relevant config sections that are referenced in that post to see if something helps to get a hint why EF is not behaving as you expect. – David Tansey May 20 '16 at 02:45
  • oh, but I go to see the build output and I don't see that error in there, so not sure where to look for the innerException :-/ – knocte May 20 '16 at 03:00
  • I don't think you'll find it...The process of generating your model needs to access your MySql db, but the provider is messed up and it can't connect, the inner exception is buried in there where you probably cannot get at it. But if you can get the config straightened out then you are probably good-to-go for generating your model. – David Tansey May 20 '16 at 03:17
  • I've fixed the latter issue by installing the .NET connector from MySQL website, but it still generates faulty enums :( – knocte May 20 '16 at 07:10
  • Simple question - did you double-check the row for Document type to make sure that the RegisterDoc entry does not possibly have a null where you expect a 3? Seems highly unlikely but easy-to-check. Your question is worded like there are other mis-generated enums. Is that the case? If so, does the problem have a pattern, for example: last element is always blank? – David Tansey May 21 '16 at 02:02
  • exactly, all enums have the last element always blank – knocte May 21 '16 at 06:49
  • Hi @knocte, did you ever resolve this issue? We're hitting the exact same problem. It's only on one of our machines, so it looks like it's something in the setup. Could it be culture related? – Dirk Boer Sep 09 '16 at 13:03
  • Haven't worked on this for a while so not sure my machine still generates the faulty enums... but other colleagues don't have this problem – knocte Sep 10 '16 at 02:03
  • Exactly the same problem here, VS2013 generated it correct earlier today but after installing VS2015 and generating the code, all my enums are missing the last element's value. This problem does not occur on my other development machines. I guess EF doesn't like a fresh VS2015? :( – Fixation Dec 15 '16 at 16:06
  • my workaround is committing the generated files from the VS instances that generate them well :( – knocte Dec 15 '16 at 17:08

2 Answers2

7

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.

Alex Butenko
  • 3,664
  • 3
  • 35
  • 54
  • 2
    Excellent. FYI you can open the *.tt file(s) in VS and change the line endings to `CRLF` at the bottom-right of the document: https://i.imgur.com/EFc2Lz5.png – Josh M. Apr 14 '20 at 15:10
  • I can confirm this is still a problem in VS2019 and EF 6.2. This is the fix. – Gabriel Luci Jun 01 '20 at 21:45
2

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.

Fixation
  • 969
  • 6
  • 12
  • sorry but where does this code come from? how do you locate it in VS? do you know if this is part of EF? – knocte Dec 20 '16 at 12:40
  • I use a model-first approach, in an empty project add a Entity Data Model (.edmx), open the model (Named Model1.edmx) and right click > Add Code Generation Item. This will add a Model1.tt item in your solution explorer tree beneath your Model1.edmx, the code i'm speaking of is inside this Model1.tt file. – Fixation Dec 20 '16 at 12:50
  • I'm not sure which VS plugin injects the code into this Model1.tt file to begin with, it's probably EF. I'm still hoping to find an answer along the lines of 'Update your XX to version XX' – Fixation Dec 20 '16 at 13:01
  • EF is opensource so we should be able to find the code that does this – knocte Dec 20 '16 at 14:56