2012-07-17 6 views
1

MVC 4를 실행하는 푸른 웹 응용 프로그램이 있습니다. 엔티티 프레임 워크 (버전 4.3.1.0)와 코드 우선을 데이터 컨텍스트와 함께 사용합니다."컨트롤러 추가"문제, MVC 4

모든 프로젝트 파일에는 데이터 파일이 있고 모든 모델 파일도 있습니다.

public class AwesomeModelContext : DbContext 
{ 

    public DbSet<User> Users { get; set; } 
    public DbSet<License> Licenses { get; set; } 
    public DbSet<AppSession> AppSessions { get; set; } 
    public DbSet<EditSession> EditSessions { get; set; } 
    public DbSet<Space> Spaces { get; set; } 
    public DbSet<SpaceUserPrivilege> SpaceUserPrivileges { get; set; } 
    public DbSet<File> Files { get; set; } 
    public DbSet<Resource> Resources { get; set; } 
    public DbSet<Team> Teams { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 

     // Remove cascading deletes, having them turned on by default scares me. 
     modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 

     modelBuilder.Entity<Space>() 
      .HasMany<SpaceUserPrivilege>(s => s.SpaceUserPrivileges) 
      .WithRequired(p => p.Space) 
      .WillCascadeOnDelete(true); 
     modelBuilder.Entity<User>() 
      .HasMany<SpaceUserPrivilege>(u => u.SpaceUserPrivileges) 
      .WithRequired(p => p.User) 
      .WillCascadeOnDelete(true); 
     modelBuilder.Entity<Team>() 
      .HasMany<User>(u => u.Users) 
      .WithRequired() 
      .WillCascadeOnDelete(false); 
     // Run migrations, if any. 
     Database.SetInitializer<AwesomeModelContext >(new MigrateDatabaseToLatestVersion<AwesomeModelContext , Configuration>()); 
    } 

} 

관련 모델은 "팀"이며 다음과 같습니다

namespace MyAwesomeNamespace.Model 
{ 
    public class Team 
    { 

     [Key] 
     public int Id { get; set; } 
     public string Name { get; set; } 

     public ICollection<User> Users { get; private set; } 

    } 
} 

그게 아니라 모든 좋은해야한다. 이들은 내 전체 웹 응용 프로그램에 대한 모델입니다. 이제 컨트롤러를 추가하여 실제로 모델을 사용할 수 있습니다. 그래서 저는 평소에 무엇을합니까?

Right-click the controller folder, and press "add controller"

I name the controller "TeamController", then select the model to be "MyAwesomeNamespace.Model.Team" and the data context to "MyAwesomeNamespace.Model.AwesomeModelContext".

이제 Add를 누르십시오. 결과는 다음과 같습니다.

"Unable to retrieve metadata for 'MyAwesomeNamespace.Model.Team'. Invalid column name 'CreatedOn'."

누구나 여기서 무엇을 해야할지 알고 계십니까? 나는 이것에 대한 해결책을 찾을 수 없다.


일부 추가 정보 :

(온라인) 푸른 데이터베이스를 사용. Visual Studio 2010 Pro 사용.

+0

또한 엔티티 프레임 워크 5.0.0-rc를 사용해 보았습니다. "... Model.Team '에 대한 메타 데이터를 검색 할 수 없습니다.'FK_dbo.TeamUsers_dbo.Teams_Team_Id '는 제약 조건이 아니며 제약 조건을 삭제할 수 없습니다. 이전 오류를 참조하십시오." – Automatico

답변

1

데이터베이스의 팀 테이블에 CreatedOn 열이 있는지 여부를 알고 싶습니다. 이미 데이터베이스가있는 경우 모델 클래스가 데이터베이스 테이블과 일치하는지 확인하십시오. 예를 들어 모델에 CreatedOn 속성을 추가합니다. 코드를 처음으로 사용하려는 경우 데이터베이스를 제거 할 수 있습니다. Entity Framework에서 자동으로 데이터베이스를 생성합니다. 데이터베이스를 먼저 접근 할 수도 있습니다. Entity Framework에서 자동으로 데이터베이스의 코드를 생성합니다.

최고 감사합니다,

명나라 쑤.

+0

이 문제를 해결하기 위해 두 가지 일을했습니다. 모든 프로젝트에서 엔티티 프레임 워크를 다시 설치하고 데이터베이스를 삭제하고 다시 작성했습니다. 이것은 일했다 :) – Automatico

0

EF4. *에서 EF 5.0으로 업그레이드 할 때 마이그레이션이 활성화 된 상태에서 EF 코드의 항목처럼 보입니다. MiniProfiler와 함께 사용하면됩니다. 테이블은 시스템 테이블 아래에 dbo._MigrationHistory에있었습니다.

당신은 몇 일을 시도 :

  1. 당신은 시스템 테이블 폴더 아래에 dbo._MigrationHistory 테이블에 수동으로 CreatedOn (날짜 시간) 열을 추가 할 수 있습니다.
  2. Configuration.AutoDetectChangesEnabled = false;
  3. 이 줄을 MiniProfilerEF.Initialize()으로 주석 처리하여 EF 프로파일 링을 비활성화 할 수 있습니다.