2017-04-16 131 views
0

我正在開發與剃刀語法和實體框架6的asp.net mvc 5 intranet應用程序,但是當我運行該應用程序時,發生EntitySqlException實體框架6與SQL Server 2012錯誤拋出EntitySqlException

當我看該錯誤消息,然後我有這樣的:

的實體集或函數導入「COVENANTS_RT」不是在實體容器「型號1」限定。

這裏是我的型號1 DbContext

public partial class Model1 : DbContext 
{ 
    public Model1() : base("name=Model1") 
    { 
    } 

    public virtual DbSet<covenants> covenants { get; set; } 
    public virtual DbSet<covenants_detalhes> covenants_detalhes { get; set; } 
    public virtual DbSet<COVENANTS_RT> COVENANTS_RT { get; set; } 
    public virtual DbSet<RT> RT { get; set; } 
    public virtual DbSet<VW_USUARIOS> VW_USUARIOS { get; set; } 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
    ... 
    modelBuilder.Entity<RT>() 
       .Property(e => e.nome_operacao) 
       .IsUnicode(false); 

    modelBuilder.Entity<RT>() 
       .HasMany(e => e.COVENANTS_RT) 
       .WithRequired(e => e.RT) 
       .HasForeignKey(e => e.FK_COD_RT); 

這裏是我的模型類COVENANTS_RT

[Table("COVENANTS_RT")] 
public partial class COVENANTS_RT 
{ 
     [Key] 
     public int CODIGO_COVENANT_PK { get; set; } 

     [Column(TypeName = "ntext")] 
     public string texto_covenant { get; set; } 

     [StringLength(255)] 
     public string situacao { get; set; } 

     [Column(TypeName = "ntext")] 
     public string OBS { get; set; } 

     public int FK_COD_RT { get; set; } 

     public virtual RT RT { get; set; } 
} 

和類RT

[Table("RT")] 
public partial class RT 
{ 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public RT() 
     { 
      COVENANTS_RT = new HashSet<COVENANTS_RT>(); 
     } 

     [Key] 
     public int PK_CODIGO_RT { get; set; } 

     ... 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<COVENANTS_RT> COVENANTS_RT { get; set; } 
} 

和代碼在剃鬚刀頁面發生錯誤:

@model site_covenants.Models.RT 
@{ 
    Layout = null; 
} 
... 

@foreach (var Item in Model.COVENANTS_RT) 

那麼,我需要做些什麼來解決這個問題?

回答

0

這行代碼public partial class Model1 : DbContext實際上是你的DbContext而不是一個模型。相應地命名您的對象總是更好。

例如:public partial class ApplicationDbContext : DbContext inseatd Model1

爲了使用流利的API,你需要重寫OnModelCreating方法:

public partial class ApplicationDbContext : DbContext 
{ 
    public virtual DbSet<covenants> covenants { get; set; } 
    public virtual DbSet<covenants_detalhes> covenants_detalhes { get; set; } 
    public virtual DbSet<COVENANTS_RT> COVENANTS_RT { get; set; } 
    public virtual DbSet<RT> RT { get; set; } 
    public virtual DbSet<VW_USUARIOS> VW_USUARIOS { get; set; } 

    public ApplicationDbContext() 
     : base("name=Model1") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<RT>() 
      .Property(e => e.nome_operacao) 
      .IsUnicode(false); 

     modelBuilder.Entity<RT>() 
      .HasMany(e => e.COVENANTS_RT) 
      .WithRequired(e => e.RT) 
      .HasForeignKey(e => e.FK_COD_RT); 
    } 
} 
+0

是的,我忘了複製所有的類來解問題。但我編輯過。 –