2014-02-24 21 views
2

所以我试图使用MVC 4互联网应用程序模板和UserProfile数据库表为它创建帐户,然后添加表具有依赖于UserProfile表格以获取更多信息。实体框架codefirst issue无法确定类型之间的关联主体端

该模型将UserProfile 0 ---> 1 UserType1 and UserProfile 0 ----> UserType2 在USERPROFILE表可能有UserType1依赖纪录,可能有相关纪录UserType2,如果有任一UserType1UserType2一个条目的主键是一个外键是UserId从用户配置文件

的POCO是:

public class UserProfile 
{ 

    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 
    public string Email { get; set; } 
    public string UserName { get; set; } 
    public int type { get; set; } 
    public virtual UserType1 UserType1 { get; set; } 
    public virtual UserType2 UserType2 { get; set; } 

public class UserType1 
{ 
    [key,ForeignKey("UserProfile")] 
    public virtual int UserId {get;set;} 
    public int myval {get;set;} 
    public UserProfile UserProfile {get; set;} 
} 

public class UserType2 //same as usertype 1 

我试着将模型映射语句,但无济于事

为用户配置模型映射数据:

public class UserProfileMap : EntityTypeConfiguration<UserProfile> 
{ 
    public UserProfileMap() 
    { 
     // Primary Key 
     this.HasKey(t => t.UserId); 

     // Properties 
     this.Property(t => t.Email) 
      .IsRequired() 
      .HasMaxLength(56); 

     this.Property(t => t.UserName) 
      .IsRequired() 
      .HasMaxLength(50); 

     // Table & Column Mappings 
     this.ToTable("UserProfile"); 
     this.Property(t => t.UserId).HasColumnName("UserId"); 
     this.Property(t => t.Email).HasColumnName("Email"); 
     this.Property(t => t.UserName).HasColumnName("UserName"); 
     this.Property(t => t.UserType).HasColumnName("UserType"); 

     this.HasOptional(e => e.UserType1).WithRequired(); 

为usertypes模型映射的数据是这样的:

public class UserType1 : EntityTypeConfiguration<UserType1> 
{ 
    public UserType1Map() 
    { 
     // Primary Key 
     this.HasKey(t => t.UserId); 

     // Properties 
     this.Property(t => t.UserId) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

     this.HasRequired(t => t.UserProfile).WithOptional(); 


     this.Property(t => t.Company) 
      .IsRequired() 
      .HasMaxLength(50); 

     // Table & Column Mappings 
     this.ToTable("UserType1"); 
     this.Property(t => t.UserId).HasColumnName("UserId"); 
     this.Property(t => t.Company).HasColumnName("Company"); 


     // Relationships 
     this.HasRequired(t => t.UserProfile).WithOptional(); 

    } 
} 

但我总是得到这个错误无法确定之间的关联的主要终点类型'myApp.Models.UserType1'和'myApp.Models.UserProfile'。该关联的主要目的必须使用关系流畅API或数据注释来显式配置。

我错过了什么?

回答

2

配置关系只有一个实体(在你的情况下,在UserProfileMap)。明确指定.WithRequired()调用中的属性。下面是为我工作的演示代码:

modelBuilder.Entity<SharedKeyRequired>() 
    .HasOptional(skr => skr.SharedKeyOptional) 
    .WithRequired(sko => sko.SharedKeyRequired); 


public class SharedKeyRequired 
{ 
    public int SharedKeyRequiredId { get; set; } 

    public virtual SharedKeyOptional SharedKeyOptional { get; set; } 
} 

public class SharedKeyOptional 
{ 
    public int SharedKeyOptionalId { get; set; } 

    public virtual SharedKeyRequired SharedKeyRequired { get; set; } 
} 
+0

谢谢!我必须从你提供的内容中做出一点小改变。实体框架不喜欢SharedKeyOptionalClass上没有外键的事实。我使用数据符号并在sko中为int值添加了[Key,ForeignKey(“SharedKeyRequired”)]。 – Himilou

0

莫霍面,我投你的答案是正确的,但我想我会把MVC等效源这里那些可能由verbage混淆。

期望的最终结果是使用MVC AccountModel并添加代码优先的表,其具有外键作为其主键来与可选的1对1的关系

延长用户配置表修改您的用户配置文件的类添加虚拟引用您的新表

[Table("UserProfile")] 
public class UserProfile 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 
    public string UserName { get; set; } 

    public virtual UserInfo UserInfo { get; set; } 
} 

创建新表,我选择用数据符号,你可以在模型构建器将其指定为 modelBuilder.Entity()HasKey(K => k.UserId)。 modelBuilder.Entity()。Property(ui => ui.UserId) .HasDatabaseGeneratedOption(DatabaseGeneratedOption。没有);

public class UserInfo 
{ 
    [Key,ForeignKey("UserProfile")] //use above or specify this 
    public int UserId { get; set; } 
    public virtual UserProfile UserProfile { get; set; } 
    public int somevalue { get; set; } 
    public string name { get; set; } 

} 

覆盖你上下文类的你的OnModelCreating构件并指定关系作为莫霍指出 公共类UsersContext:的DbContext { 公共UsersContext() :基部( “DefaultConnection”) { }

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     // modelBuilder.Entity<UserInfo>().HasKey(k => k.UserId); 
     // modelBuilder.Entity<UserInfo>().Property(ui => ui.UserId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 
     modelBuilder.Entity<UserProfile>().HasOptional(ui => ui.UserInfo).WithRequired(up => up.UserProfile); 
     base.OnModelCreating(modelBuilder); 
    } 


    public DbSet<UserProfile> UserProfiles { get; set; } 
    public DbSet<UserInfo> UserInfoes { get; set; } 
} 
相关问题