我有两个表:用户和个人资料
public class User
{
public string Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public Profile Profile { get; set; }
}
public class Profile
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public DateTime Created { get; set; }
public User User { get; set; }
}
配置部分:
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
ToTable( "Users" );
HasKey( p => p.Id );
Property( p => p.UserName ).HasMaxLength( 100 );
Property( p => p.UserName ).HasColumnAnnotation( "Index",
new IndexAnnotation( new IndexAttribute( "IX_UserName" )
{
IsUnique = true
} ) ).IsRequired();
// Add code here to relate to Profile table and what code?
...
}
}
public class ProfileConfiguration : EntityTypeConfiguration<Profile>
{
public ProfileConfiguration()
{
ToTable( "Profiles" );
HasKey( p => p.UserName );
Property( p => p.FirstName ).HasMaxLength( 100 );
Property( p => p.SecondName ).HasMaxLength( 100 );
Property( p => p.Created ).IsRequired();
// Or add code here to relate to User table and what code?
...
}
}
我需要将两个一对一的表相关联。如何将主键指定为引用 User.UserName 的外键 Profile.UserName ,而不是 User.Id 如何使用Fluent API实现?