What's the right way to suppress EF warnings?

喜夏-厌秋 提交于 2020-05-08 06:35:19

问题


I would like to suppress these warnings but I could not figure out how to do it.

suppress


回答1:


Those are not warnings you are seeing, those are Errors. The output tells you very clearly when a diagnostic message is a warning or an error. You've given the compiler an error number (6002) and told it to suppress the warning with that number, but such a warning does not exist. So, you've only succeeded in creating an additional error

In this case, the message you're trying to suppress is a fatal compiler error; your only option is to fix your code. You must define a primary key for every Entity Framework object, or the Entity Framework will not work with it. The wording of the error is a bit confusing, as it implies that somehow the compiler has "worked around" your problem, but that's not the case.

For more details about that specific error, and how to fix it:

Error 6002: The table/view does not have a primary key defined




回答2:


You are correct that these are warnings, not errors. The easiest way to supress the validation warnings from the compiler is to disable the Validate on Build property of the EF model. To do so, open your .edmx and select the background. Open the Properties window of Visual Studio and set Validate on Build to false. When you want to validate the model, just open the model again. There is also a right click context menu option on the model to Validate.




回答3:


With Visual Studio 2019, you can right click the error and suppress this. This will create an .editorconfig file with for example the following contents:

dotnet_diagnostics.EF1000.severity = suggestion




回答4:


Even better would be to create a .editorconfig file yourself, either at the solution level or at the project level, and add rules such as Erik has mentioned in his post. As mentioned in this answer, you can add an EditorConfig file, which will already bring some other rules and good practices into your project or solution, if you pick the .NET option.


  • From the menu bar, choose Project > Add New Item; or press Ctrl+Shift+A

  • Select the editorconfig File (.NET) template to add an EditorConfig file prepopulated with default .NET code style, formatting, and naming conventions


However, to completely suppress warnings or suggestions, you should use 'none' instead of 'suggestion'. For example, for the CA1707, which shows a warning every time a method name contains an '_', you would need to add the following entry in the .editorconfig file:

[*.cs]
dotnet_diagnostic.CA1707.severity = none

However, I do like this rule. Except when applied in Test projects, which by convention end up with 'Tests.cs'. I could then refine my rule, to consider that case:

[*Tests.cs]
dotnet_diagnostics.CA1707.severity = none

With the latest rule, I can then go for a solution level .editorconfig file, which will apply that rule only to Test projects.



来源:https://stackoverflow.com/questions/29337130/whats-the-right-way-to-suppress-ef-warnings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!