【发布时间】:2013-06-24 17:29:02
【问题描述】:
我以前在我的数据库中创建了一些表,我想将它们映射到一些模型类:
"SISTEMA.Voyages" => public class Voyage
"SISTEMA.Puerto" => public class Port
我知道在带有实体框架的 ASP.MVC 4 中,这可以通过两种方式之一来完成。但是对于他们两个,我都遇到了我不知道如何解决的错误。
Voyage.cs 中的第一个:
[Table("SISTEMA.Voyages")]
public class Voyage
或者 Context.cs 中的第二个:
public class ApplicationEntities : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Voyage>().ToTable("SISTEMA.Voyages");
}
}
对于第一个版本,当我之前认为这是自动的时,我得到了这个错误:
The type or namespace name 'Table' could not be found (are you using directive or an assembly reference?)
The type or namespace name 'TableAttribute' could not be found (are you using directive or an assembly reference?)
我收到了这个我没想到的错误,因为我认为这是一个配置问题,让我很困惑:
The model backing the 'ApplicationEntities' context has changed since the database was created. Consider using Code First Migrations to update the Database.
这段历史记录在哪里?
为了记录,我习惯于在 Rails 中通过输入以下内容来处理此类问题:
class Voyage
self.table_name = "SISTEMA.Voyages"
end
而且我对 C#/ASP.NET 不是很熟悉。只是发布我接下来一小时要查找的内容,除非有人先告诉我哪里出错了。
【问题讨论】:
-
第一个版本你是否添加了 using 指令? (使用 System.ComponentModel.DataAnnotations)
-
我刚做了。谢谢。我现在得到了第二种情况发生的错误。 :( 正在研究迁移,但我仍然感到困惑......
-
阅读后更接近解决方案:stackoverflow.com/questions/3600175/… 基本上我添加了
using System.Data.Entity;在Global.asax中还添加了Database.SetInitilaizer<DataContext>(null); -
现在我只需要正确的语法,因为它被解释为
dbo.SISTEMA.Voyages而不是SISTEMA.Voyages
标签: asp.net-mvc-4