DateTime Does Not Exist After Ado.NET Mocking Generator

痞子三分冷 提交于 2019-12-11 04:55:35

问题


I am using the ADO.NET Mocking Context Generator to generate my entity classes from an EDMX file, so that I can use them in unit tests. However, after I generate my entities and try to build the project, I get the following error:

The type name 'DateTime' does not exist in the type 'MyProject.Models.System'

Within the code, DateTime properties are declared in the format:

public virtual System.DateTime LastActive

If I change System.DateTime to just DateTime, the error clears. Unfortunately this is not practical, as there quite a lot of them, besides they will be overwritten next time I regenerate.

Why am i receiving this error, and how can I prevent it?


回答1:


It looks like the problem is that you've got a type called System. That's a really bad idea - it's going to cause this problem all over the place. (See Eric Lippert's blog posts on this topic for more details...)

The most specific way of declaring this would be:

public virtual global::System.DateTime LastActive { get; set; }

If you could change the generator to create that, it should be okay... but personally I'd just change the System type to be called something else if you possibly can.




回答2:


Recently had the same problem and stumbled upon this, Jon Skeets answer helped me identify the problem, however there's a much simpler solution than editing the in the *.tt files, if you go to the designer you can call your entity/class/model something else than system while still having a table called system.

For me changing the table name was not an option since I did not provide the database, I was just coding to it.

  1. Open the .edmx file in the designer

  2. find the table named system

  3. right-click and select properties or click it and look at the lower right corner

  4. Under the category "general" find the property "name" and call it something different than table.

  5. rightclick the .edmx file and select "run custom tool"

For me this solved 13 000+ errors

If anyone knows how to change the name of an entity without using the designer write it in the comments if you want.



来源:https://stackoverflow.com/questions/10815019/datetime-does-not-exist-after-ado-net-mocking-generator

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