所以我试图使用MVC 4互联网应用程序模板和UserProfile
数据库表为它创建帐户,然后添加表具有依赖于UserProfile
表格以获取更多信息。实体框架codefirst issue无法确定类型之间的关联主体端
该模型将UserProfile 0 ---> 1 UserType1 and UserProfile 0 ----> UserType2
在USERPROFILE表可能有UserType1
依赖纪录,可能有相关纪录UserType2
,如果有任一UserType1
或UserType2
一个条目的主键是一个外键是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或数据注释来显式配置。
我错过了什么?
谢谢!我必须从你提供的内容中做出一点小改变。实体框架不喜欢SharedKeyOptionalClass上没有外键的事实。我使用数据符号并在sko中为int值添加了[Key,ForeignKey(“SharedKeyRequired”)]。 – Himilou