如何在“代码优先”方法中配置一对多关系?

时间:2018-10-02 05:01:32

标签: c#

帐户模型类别:

  [Table("Accounts")]
    public class Account
    {
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public float Interval { get; set; }
    }

移动模型类:

public class Mobile
    {

        public int Id { get; set; }
        public string MobileNo { get; set; }
        public virtual Account Account { get; set; }

        public static Mobile Add(string mobile)
        {
            Mobile mobiles = new Mobile();
            mobiles.MobileNo = mobile;

            return mobiles;
        }

    }

这是我保存新条目的动作:

   public ActionResult Register(CompanyAccountViewModel model)
    {
        CancellationTokenSource cancelToken = new CancellationTokenSource();
        CompanyAccountRegistrationRequestMessage requestMessage = new CompanyAccountRegistrationRequestMessage();
        requestMessage.CompanyName = model.Name;
        requestMessage.Interval = model.Interval;
        model.Mobile.ForEach(x=> requestMessage.MobileNo.Add(x));
        Task<CompanyAccountRegistrationResponseMessage> response = _interactor.Handle(requestMessage, cancelToken.Token);

        CompanyAccountViewModel viewModel = _presenter.Handle(response.Result, model, ModelState);
        return View(viewModel);
    }

_presenter.Handle()方法:

  public Task<CompanyAccountRegistrationResponseMessage> Handle(CompanyAccountRegistrationRequestMessage request, CancellationToken cancellationToken)
        {
            CompanyAccountRegistrationResponseMessage returnMessage;
            var validationResult = _validator.Validate(request);
            using (PowerSupplyDBContext dbContext = new PowerSupplyDBContext())
            {
                // ILoginUserCreator loginUsercreator = new LoginUserCreator();
                ICompanyAccountRegistrationUseCase companyAccountregistrationUseCase = new CompanyAccountRegistrationUseCase(dbContext);
                companyAccountregistrationUseCase.RegisterCompanyAccount(request);
                dbContext.SaveChanges();
            }
            returnMessage = new CompanyAccountRegistrationResponseMessage(validationResult);
            return Task.FromResult<CompanyAccountRegistrationResponseMessage>(returnMessage);
        }

我现在跳过验证,即:未设置验证:

public class CompanyAccountRegistrationRequestMessageValidator : AbstractValidator<CompanyAccountRegistrationRequestMessage>
{
    public CompanyAccountRegistrationRequestMessageValidator()
    {

    }
}

期望: 帐户 手机之间存在一对多关系 表,即:一个帐户有很多手机号码。因此, 移动 表中有一个Account_Id,并且此Account_Id应该用插入的帐户ID填充。

实际发生的情况: Account_Id 手机 表格为空。

enter image description here

Accounts table

enter image description here

Mobiles table

我在保存之前缺少要映射的内容,这使我度过了漫长的一天。我刚刚启动ASP.NET,如果我的问题不是这里要提出的标准,我深表歉意。谢谢你的时间。

修改 companyAccountregistrationUseCase.RegisterCompanyAccount(request)

 public void RegisterCompanyAccount(CompanyAccountRegistrationRequestMessage request)
    {
        IList<Domain.Contacts.Mobile> mobiles = new List<Domain.Contacts.Mobile>();

        _companyAccountRegistrationRepo = new CompanyAccountRegistrationRepository(_dbContext);


        Account account = new Account();
        account.CompanyName = request.CompanyName;
        account.Interval = request.Interval;


        foreach (string mobile in request.MobileNo)
        {
            Domain.Contacts.Mobile mobileNo = Domain.Contacts.Mobile.Add(mobile);
            // contact.AssignAccountUser(accountUser);
            mobiles.Add(mobileNo);
        }

        _companyAccountRegistrationRepo.RegisterCompanyAccount(account, mobiles);
    }

1 个答案:

答案 0 :(得分:1)

在将数据保存到数据库之前,必须分配Account实例的Mobile属性。否则,移动实体应如何知道与之关联的帐户?

因此,您应该使用foreach方法将以下内容添加到RegisterCompanyAccount循环中:

mobileNo.Account = account;