【问题标题】:Mapping View To MVC Code first首先将视图映射到 MVC 代码
【发布时间】:2016-08-30 11:56:25
【问题描述】:

我在 SQL 中有一个名为 ViewTest 的视图。

在代码中我有这个模型

    [Table("dbo.ViewTest")]
    public class ViewTest
    {
        public int Id { get; set; }

        public int EmployeeID { get; set; }
        public string PreferredName { get; set; }
        public string EmailPrimaryWork { get; set; }
        public string GeoCoverage { get; set; }
        public string Role { get; set; }
        public bool? LeftEmpFlag { get; set; }
    }

在配置文件中:

public virtual IDbSet<ViewTest> ViewTests { get; set; }


protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new ViewConfiguration());
        }

 public class ViewConfiguration : EntityTypeConfiguration<ViewTest>
        {
            public ViewConfiguration()
            {
                this.HasKey(t => t.Id);
                this.ToTable("ViewTest");
            }
        }

我想有这样一个 ViewTests.Where(....) 的连接,但是当我尝试这样做时,我遇到了错误 数据库中已经有一个名为“ViewTest”的对象。。这意味着实体框架尝试创建新表,我不想要这个。我只想访问这个视图!

【问题讨论】:

    标签: asp.net-mvc code-first entity-framework-migrations sql-view


    【解决方案1】:

    嗯,它是代码优先,所以你可能应该create your view in code。如果这不可能,那么您需要通过在迁移的 Up() 方法中注释该行来告诉 EF 不要创建表。一旦你更新数据库 EF 将在元数据中包含它,你应该很高兴。

    【讨论】:

      猜你喜欢
      • 2011-04-05
      • 1970-01-01
      • 2013-12-19
      • 2011-10-26
      • 2015-02-02
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多