.Net 一对一的双向关联Map写法

       systemuser  与 systemuserlogin  为1对1的关联关系:

       systemuserMap:  HasOne<SystemUserLogin>(o => o.SystemUserLogin).Cascade.All().PropertyRef("SystemUser"); 

       systemuserloginMap:References<SystemUser>(o => o.SystemUser).Column("SystemUserID").Unique(); //.Unique()对“SystemUserID”进行了唯一性限定

       注意:此处Map文件中生成的1对1 的关联关系当其存储到数据库中时,才会产生。

      例子:

  [Test]
        public void Test_user_loginuser_findeach() 
        {


            IRepository<SystemUser> repos = UnityHelper.UnityToT<IRepository<SystemUser>>();
            SystemUser s = new SystemUser()
            {
                Name = "findpangfuxing",
                Email = "findpang@126.com",
                Description = "findtest"
            };
            repos.SaveOrUpdate(s);

            IRepository<SystemUserLogin> reposl = UnityHelper.UnityToT<IRepository<SystemUserLogin>>();
            SystemUserLogin sl = new SystemUserLogin()
            {
                LoginName = "findpfx",
                Password = "findpang@126.com",
                SystemUser = s
            };
            reposl.SaveOrUpdate(sl);

            SystemUserLogin suff = new SystemUserLogin();
            //测试1:
            suff = s.SystemUserLogin;       //运行至此处,发现  s.SystemUserLogin=null
            suff = repos.Load(s.Id).SystemUserLogin;  //运行至此处,发现  s.SystemUserLogin=null
            //原因是,此处在运行时,检测到内存中s对象已存在,故直接从内存数据中读取s对象,未从数据库中读取

            //测试2:
            suff = repos.Load("b756ab67-893c-4391-8a9b-b912b64dedda").SystemUserLogin;  //发现可以取到 s.SystemUserLogin
            //原因是,从数据库中读取的对象
            string a = suff.LoginName;
        }
原文地址:https://www.cnblogs.com/wukong0214/p/2879501.html