实体类上的一对一关系需要手动指定

public class User
    {
        public long UserId { get; set; }
        public string UserName { get; set; }
        public string UserPwd { get; set; } 
        public DateTime UserCreateDate { get; set; }
        public DateTime LastLoginDate { get; set; }
        public DateTime LastActivityDate { get; set; }
        public int UserRoleId { get; set; }

        public virtual UserRole UserRole { get; set; }
        public virtual UserProfile UserProfile { get; set; }
    }
public class UserRole
    {
        public int UserRoleId { get; set; }
        public string RoleName { get; set; }
    }
public class UserProfile
    {
        [Key, ForeignKey("User")]
        public long UserId { get; set; }
        public int Gender { get; set; }
        public string NickName { get; set; }
        public string Signature { get; set; }
        public string Intro { get; set; }
        public DateTime? Birth { get; set; }
        public string Location { get; set; }
        public string Website { get; set; }
        public string Qq { get; set; }
        public string WeiBo { get; set; }
        public string Medals { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public bool IsSendEmail { get; set; }

        public virtual User User { get; set; }
    }

一对多的关系可以不用手动指定,就可以直接使用,但是一对一的关系要手动指定后才能进行使用,这里使用了Data Annotation方式配置。添加了[Key, ForeignKey("User")]表明在UserProfile中UserId既是主键,又是来自于"User"的外键。

否则会出现异常

Unable to determine the principal end of an association between the types ‘Model.User’ and ‘Model.UserProfile’. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

转载于:https://www.cnblogs.com/brown-birds/p/4062387.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值