实体框架6.0使用代码优先Asp.NET MVC 5映射Oracle现有视图

时间:2016-04-29 08:06:15

标签: c# asp.net asp.net-mvc oracle entity-framework

我试图首先使用代码将实体框架映射到oracle视图。这是我的代码:

Hospital.cs模型

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Entities
{
    public class Hospital
    {   

        [Key]
        public string SBRS_KURUM_NO { get; set; }

        public string PAYDAS { get; set; }

        public string KURUM_ADI { get; set; }

        public int KURUM_KODU { get; set; }

        public string KURUM_ILI { get; set; }

        public string KURUM_ILCESI { get; set; }

        public string KURUM_TUR_ADI { get; set; }

        public string KURUM_TIPI { get; set; }

        public string IL_KODU { get; set; }

        public int ILCE_KODU { get; set; }

        public string AKTIF { get; set; }

        public string KURUM_TUR_KODU { get; set; }

        public DateTime GUNCELLEME_TARIHI { get; set; }

        public string BASAMAK { get; set; }

        public int YATAK_UNIT_SAYISI { get; set; }

        public string HASTANE_ROLU { get; set; }

        public int BAGLI_BIRIM { get; set; }

        public string KURUM_ROLU { get; set; }
    }
}

HospitalMap.cs Mapping Class

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Entities.Mappings
{

    public class HospitalMap : EntityTypeConfiguration<Hospital>
    {
        public HospitalMap()
        {
            this.HasKey(t => t.SBRS_KURUM_NO);

            this.Property(t => t.SBRS_KURUM_NO).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.ToTable("HOSPITALS");

            this.Property(t => t.SBRS_KURUM_NO).HasColumnName("SBRS_KURUM_NO");
            this.Property(t => t.PAYDAS).HasColumnName("PAYDAS");
            this.Property(t => t.KURUM_ADI).HasColumnName("KURUM_ADI");
            this.Property(t => t.KURUM_KODU).HasColumnName("KURUM_KODU");
            this.Property(t => t.KURUM_ILI).HasColumnName("KURUM_ILI");
            this.Property(t => t.KURUM_ILCESI).HasColumnName("KURUM_ILCESI");
            this.Property(t => t.KURUM_TUR_ADI).HasColumnName("KURUM_TUR_ADI");
            this.Property(t => t.KURUM_TIPI).HasColumnName("KURUM_TIPI");
            this.Property(t => t.IL_KODU).HasColumnName("IL_KODU");
            this.Property(t => t.ILCE_KODU).HasColumnName("ILCE_KODU");
            this.Property(t => t.AKTIF).HasColumnName("AKTIF");
            this.Property(t => t.KURUM_TUR_KODU).HasColumnName("KURUM_TUR_KODU");
            this.Property(t => t.GUNCELLEME_TARIHI).HasColumnName("GUNCELLEME_TARIHI");
            this.Property(t => t.BASAMAK).HasColumnName("BASAMAK");
            this.Property(t => t.YATAK_UNIT_SAYISI).HasColumnName("YATAK_UNIT_SAYISI");
            this.Property(t => t.HASTANE_ROLU).HasColumnName("HASTANE_ROLU");
            this.Property(t => t.BAGLI_BIRIM).HasColumnName("BAGLI_BIRIM");
            this.Property(t => t.KURUM_ROLU).HasColumnName("KURUM_ROLU");
        }
    }
}

我的背景

using System.Configuration;
using Domain.Entities;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.EntityFramework;
using System.Collections.Specialized;
using Domain.Entities.Mappings;

namespace Domain.Data
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class SptsOracleDbContext : DbContext
    {
        public SptsOracleDbContext()
            : base(new OracleConnection(ConfigurationManager.ConnectionStrings["SptsOracleDbContext"].ConnectionString), true)
                    { 

                    } 

        public DbSet<BranchType> BranchTypes { get; set; }
        public DbSet<Branch> Branches { get; set; }
        public DbSet<StaffStatu> StaffStatus { get; set; }
        public DbSet<ManagerialTitle> ManagerialTitles { get; set; }
        public DbSet<AcademicTitle> AcademicTitles { get; set; }
        public DbSet<Hospital> Hospitals { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

        base.OnModelCreating(modelBuilder);
            modelBuilder.HasDefaultSchema("SPTS");
            modelBuilder.Ignore<Hospital>();
            modelBuilder.Configurations.Add(new HospitalMap());

        }
    }
}

当我尝试使用context.Hospitals.Count()使用医院模型时,它会说出此错误:The entity type Hospital is not part of the model for the current context.

我如何解决这个问题?

非常感谢帮助

0 个答案:

没有答案