将 class 添加到 EF 模型会导致编译错误 "The type or namespace name <className> could not be found"
Adding class to EF Model cause compile error "The type or namespace name <className> could not be found"
奇怪,我已经添加了 5 table 没有问题。但是,当我尝试通过我的 edmx 模型添加我的 PostCodesUK
table 时,它给了我这个错误消息:
Error 1 The type or namespace name 'PostCodesUK' could not be found (are you missing a using directive or an assembly reference?) d:\Ben\documents\visual studio 2013\Projects\DogWalks\DogWalks\App_Code\WalksModel.Context.cs 33 30 DogWalks
不确定发生了什么,但有一些细微的差别。例如,如果我查看 WalksModel.tt
classes,我可以看到 PostCodesUK class,但它没有生成额外的 'stub':
如果我然后双击上面的错误,它会将我带到 WalksModel.Context.cs
我看到这个错误的地方:
我查看了 PostCodesUK.cs
class 下的 namespace
,它与其他 class 匹配。有谁知道问题是什么?我在网上搜索了这个问题,但找不到解决方案。
更新: 这就是 PostCodeUK class 的样子:
namespace DogWalks.App_Code
{
using System;
using System.Collections.Generic;
public partial class PostCodesUK
{
public int PostcodeID { get; set; }
public string Postcode { get; set; }
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
public string PostcodeNoSpace { get; set; }
}
}
此外, 我在我的模型中添加了另一个临时 table(人)来测试它,它也给我与 PostCodesUK 相同的错误消息:
public virtual DbSet<Comment> Comments { get; set; }
...
public virtual DbSet<PostCodesUK> PostCodesUKs { get; set; } //error
public virtual DbSet<Person> People { get; set; } //error
好吧,在花了无数个小时试图找到解决方案之后,我可能真的解决了它(碰木头)。
我很沮丧,所以我删除了我的 edmx
模型。在线阅读,我发现当 edmx 模型保存在子文件夹(而不是根目录)时,VS 2012
存在错误。因此,如果我的下面的解决方案不起作用,尽管我设法将其保存在子文件夹中,但可以尝试一下。
无论如何,删除我的模型后:
- 我将整个数据库模型重新添加到名为 DAL 的 new 文件夹中。当我尝试编译时,我注意到我所有的 类 现在都在抛出同样的错误。
- 我为所有 类 更改了所有
build action
(从 Content
到 Compile
)
- 重新构建应用程序,它运行正常。
所以为了测试我尝试了:
- 正在为我的模型添加另一个 table,
Person
。
- 尝试编译(实际上编译成功)但是当我查看 Model.tt 下的 Person.cs 文件时,我发现存根再次丢失:
- 我将
Build Action
设置更改为编译、保存、关闭 VS 并重新打开它,存根正确显示:
我不知道这是否是问题所在,但它似乎有效。我没有检查我的原始案例,但我怀疑我为我原来的 类 更改了 .cs
编译模式,但没有为新添加的 类。
我很高兴我没有把我的机器变成废金属。
奇怪,我已经添加了 5 table 没有问题。但是,当我尝试通过我的 edmx 模型添加我的 PostCodesUK
table 时,它给了我这个错误消息:
Error 1 The type or namespace name 'PostCodesUK' could not be found (are you missing a using directive or an assembly reference?) d:\Ben\documents\visual studio 2013\Projects\DogWalks\DogWalks\App_Code\WalksModel.Context.cs 33 30 DogWalks
不确定发生了什么,但有一些细微的差别。例如,如果我查看 WalksModel.tt
classes,我可以看到 PostCodesUK class,但它没有生成额外的 'stub':
如果我然后双击上面的错误,它会将我带到 WalksModel.Context.cs
我看到这个错误的地方:
我查看了 PostCodesUK.cs
class 下的 namespace
,它与其他 class 匹配。有谁知道问题是什么?我在网上搜索了这个问题,但找不到解决方案。
更新: 这就是 PostCodeUK class 的样子:
namespace DogWalks.App_Code
{
using System;
using System.Collections.Generic;
public partial class PostCodesUK
{
public int PostcodeID { get; set; }
public string Postcode { get; set; }
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
public string PostcodeNoSpace { get; set; }
}
}
此外, 我在我的模型中添加了另一个临时 table(人)来测试它,它也给我与 PostCodesUK 相同的错误消息:
public virtual DbSet<Comment> Comments { get; set; }
...
public virtual DbSet<PostCodesUK> PostCodesUKs { get; set; } //error
public virtual DbSet<Person> People { get; set; } //error
好吧,在花了无数个小时试图找到解决方案之后,我可能真的解决了它(碰木头)。
我很沮丧,所以我删除了我的 edmx
模型。在线阅读,我发现当 edmx 模型保存在子文件夹(而不是根目录)时,VS 2012
存在错误。因此,如果我的下面的解决方案不起作用,尽管我设法将其保存在子文件夹中,但可以尝试一下。
无论如何,删除我的模型后:
- 我将整个数据库模型重新添加到名为 DAL 的 new 文件夹中。当我尝试编译时,我注意到我所有的 类 现在都在抛出同样的错误。
- 我为所有 类 更改了所有
build action
(从Content
到Compile
) - 重新构建应用程序,它运行正常。
所以为了测试我尝试了:
- 正在为我的模型添加另一个 table,
Person
。 - 尝试编译(实际上编译成功)但是当我查看 Model.tt 下的 Person.cs 文件时,我发现存根再次丢失:
- 我将
Build Action
设置更改为编译、保存、关闭 VS 并重新打开它,存根正确显示:
我不知道这是否是问题所在,但它似乎有效。我没有检查我的原始案例,但我怀疑我为我原来的 类 更改了 .cs
编译模式,但没有为新添加的 类。
我很高兴我没有把我的机器变成废金属。