zoukankan      html  css  js  c++  java
  • 实体类上的一对一关系需要手动指定

    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.

  • 相关阅读:
    P2480 SDOI 古代猪文(自带其他详细基础数论)
    01 分数规划
    P2606 ZJOI2010 排列计数
    P4140 奇数国
    SHOI 2014 概率充电器
    P2157 SDOI2009 学校食堂
    分块
    斜率 优化 dp
    线段树树状数组从入门到跳楼
    Ogre::scene_blend 场景混合
  • 原文地址:https://www.cnblogs.com/brown-birds/p/4062387.html
Copyright © 2011-2022 走看看