1:(0or1)
public class User
{
public int ID { get; set; }
public string UserName { get; set; }
public string Passwd { get; set; }
public virtual UserInfo UserInfo { get; set; }
}
public class UserInfo
{
public int ID { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public virtual User User { get; set; }
public int UserID { get; set; }
}
// modelBuilder.Entity<User>().ToTable("Users"); modelBuilder.Entity<User>().HasKey(u => u.ID); modelBuilder.Entity<UserInfo>().ToTable("UserInfos"); modelBuilder.Entity<UserInfo>().HasKey(b => b.ID); ////针对“用户不一定要有信息类,信息一定要有用户类”关系的设置 modelBuilder.Entity<User>().HasOptional(b => b.UserInfo) .WithRequired(x => x.User);