2013-08-31 1 views
1

열 '아이디'에 의존 - UsersCategories을 그들은 연결됩니다 UserID에 있습니다.엔티티 프레임 워크 오류 : 개체 'PK_dbo.User은'나는이 개 테이블이 나는 5 이의 말을하자 <a href="http://www.windowsazure.com/en-us/develop/net/tutorials/web-site-with-sql-database/" rel="nofollow">ASP.NET MVC 5 tutorial</a>을 통과 MVC 기존 응용 프로그램을 다시 할

우리는 우리의 DB를위한 인증 모델을 연결할 수 있습니다 mvc5에서

, 그래서 내가

public int UserID { get; set; } 
public User User { get; set; } 
내가 mvc3/EF4에 나를 위해 일한

을 추가 Category 모델도

public ICollection<Category> Categories { get; set; } 

로 샘플을 User 모델을 업데이트 .

public class User : IUser 
{ 
    public User() 
     : this(String.Empty) 
    { 
    } 

    public User(string userName) 
    { 
     UserName = userName; 
     Id = Guid.NewGuid().ToString(); 
    } 

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

    public string UserName { get; set; } 

    public ICollection<Category> Categories { get; set; } 
} 

분류 모델

(범주 샘플 프로젝트 + 링크 1-1)

사용자 : 여기

The object 'PK_dbo.User' is dependent on column 'Id'. ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column.

내 모델입니다 : 내가 마이그레이션을 할 때 나는 오류를 얻고있다
public class Category 
{ 
    public int UserID { get; set; } 
    public User User { get; set; } 

    public int ID { get; set; } 

    public int ParentID { get; set; } 

    public string Name { get; set; } 
    //.... 
} 

문맥

public class BackendDBContext : DbContext 
{ 
    public DbSet<User> Users { get; set; } 
    public DbSet<UserSecret> Secrets { get; set; } 
    public DbSet<UserLogin> UserLogins { get; set; } 
    public DbSet<Role> Roles { get; set; } 
    public DbSet<UserRole> UserRoles { get; set; } 
    public DbSet<Category> Categories { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    } 

    protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items) 
    { 
     // 
    } 
} 
+1

사용자의'Id' 속성이 문자열 인 경우 다른 클래스의'int UserID' 속성을 사용하여 어떻게 참조 할 수 있습니까? – hvd

+0

나는 바보 야! 고맙습니다! –

+0

답으로 추가하십시오, 그래서 받아 들일 수 있습니다. –

답변

1

코멘트에서 부연 :

귀하의 User 클래스는 string 키 속성이 있습니다. 카테고리의 UserID 속성에는 동일한 유형이 있어야합니다. 그들이 아니기 때문에, 나는 EF가 UserID의 유형과 일치하도록 ID 열을 수정하려고 시도하고있는 것으로 추측하고 있습니다.이 유형은 작동하지 않아야합니다.

그러나, 추가 참고 :

Id = Guid.NewGuid().ToString(); 

를 통해 사용자 ID를 설정하고 있지만 저장을위한 자연적인 유형 Guid (uniqueidentifier SQL Server의)입니다

. 사용자의 ID 속성과 카테고리의 UserID 속성을 모두 변경할 수 있습니다.