如何修复“无法将值分配给ICollection属性”

时间:2019-07-03 10:05:38

标签: c#

我在Login(父)表和Member(子)表之间有关系。 生成模型后,我无法将值分配给子表。

  

型号:

public partial class tbl_Login
{
    public tbl_Login()
    {
        tbl_MemberDetail = new HashSet<tbl_MemberDetail>();
    }

    public Guid Id { get; set; }        
    public string login_username { get; set; }
    public string login_password { get; set; }         

    [InverseProperty("Login_")]
    public virtual ICollection<tbl_MemberDetail> tbl_MemberDetail { get; set; }
}


public partial class tbl_MemberDetail
{
    public Guid Id { get; set; }
    public string Name{ get; set; }
    public string Age{ get; set; } 
    public string DOB{ get; set; }
}

    [HttpPost]
    public async Task<IActionResult> createUser(tbl_Login account)
    {
        account.Id = Guid.NewGuid();
        account.login_username = "user01";
        account.login_password = "123456";
        account.tbl_MemberDetail.
    }

问题是,为什么我无法将值分配给account.tblmemberdetail?请帮忙!谢谢!

Problem 01

这是我的预期结果:

{
  "id": "EB94F819-1E35-447C-A6B1-14EDE4FC68BA",
  "login_username": "testuser11",
  "login_password": "123456",  
  "tbl_MemberInfo": {
    "id": "BD748329-79C0-4863-9860-DD2C70EE3750",
    "Name": "Andy",
    "Age": "30",
    "DOB": "2019 June 30"
  }
}

嗨,我重试了,看来已经可以了。这是正确的吗?

var lstMemberDetail = new tbl_MemberDetail { Id = Guid.NewGuid(), Name = "test", Age = "test", DOB ="test" }; 
tbl_Login account = new tbl_Login(); 
account.Id = Guid.NewGuid(); 
account.login_username = "user01"; 
account.login_password = "123456"; 
account.tbl_MemberInfo.Add(lstMemberInfo);

0 个答案:

没有答案