我有一个基于标准Internet应用程序模板的ASP.NET MVC 4 Web应用程序。我已经创建了一个包含所有数据库类的独立类库,并在那里移动了“UserProfile”类并稍微调整了一下,如下所示:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[HiddenInput(DisplayValue = false)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[StringLength(200)]
[Display(Name = "Email Address")]
[Required(ErrorMessage = "Please enter your email address")]
[RegularExpression(@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$", ErrorMessage = "Please enter a valid Email address")]
public string UserName { get; set; }
[StringLength(200)]
public string Name { get; set; }
}
我在我的网络应用程序中使用这个类没有问题,它已经好几个月了。
昨天我开始编写一个小助手控制台应用程序,它需要更新与Web应用程序在同一数据库中的表。我添加了对数据库类库的引用,当我使用或不使用visual studio调试器在我的开发PC上运行应用程序时,一切正常。
但是,当我将应用程序复制到我的Web服务器并从那里运行时,我收到以下错误:
Unhandled Exception: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'UserProfile' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'UserProfiles' is based on type 'UserProfile' that has no keys defined.
我删除了对EntityFramework的所有引用,并使用nuget重新安装它。我已经尝试了该框架的5.0.0和6.0.1版本。每个谷歌的错误结果都会让我发帖说要用[Key]属性来装饰UserId属性但是你可以看到我已经有了它(事实上它默认存在)
任何想法都会让我绕过弯道?
答案 0 :(得分:0)
我设法让这个工作!
我必须将对System.Web.Mvc的引用设置为“Copy Local”,以便它包含在包含可执行文件的文件夹中。
我假设在我的开发PC上有一些DLL的全局副本没有安装在服务器机器上,但是这样可行,所以我现在可以继续使用它了!