最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - Problem passing ContactEntity with relationship property EmailEntity - Stack Overflow

programmeradmin0浏览0评论

I'm following up on this question. After looking at Yong Shun's reply, I decided to experiment, and now I have some questions.

I'm new to ASP.NET Core, so bear with me.

I'm working on a WPF app with Entity Framework Core 6 calling an ASP.NET Core 6 Web API. I'm trying to create a ContactController with an AddContact method.

Here's my ContactEntity with a property of List<ContactEmailEntity> on it:

public class ContactEntity : _EntityBase
{
    private string _FirstName;

    public string FirstName
    {
        get { return _FirstName; }
        set
        {
            if (_FirstName != value)
            {
                _FirstName = value;
                RaisePropertyChanged("FirstName");
            }
        }
    }

    public List<ContactEmailEntity> ContactEmails { get; set; }

    public ContactEntity()
    {
        ContactEmails = new List<ContactEmailEntity>();
    }
}

Here's the ContactEmailEntity:

[Table("ContactEmails")]
public class ContactEmailEntity : _EntityBase
{
    private string _EmailAddress = "";

    public string EmailAddress
    {
        get { return _EmailAddress; }
        set
        {
            if (_EmailAddress != value) 
            {
                _EmailAddress = value;
                RaisePropertyChanged("EmailAddress");
            }
        }
    }

    [ForeignKey("ContactId")]
    public int? ContactId { get; set; }
    public ContactEntity? Contact { get; set; }
}

Note that the ContactEmailEntity class has a reference property to the ContactEntity.

Here's my controller method:

[Route("api/[controller]/[action]")]
[ApiController]
public class ContactController : ControllerBase
{
    [HttpPost]
    public IActionResult CreateContact([FromBody] ContactEntity myContact)
    {
        // Do some stuff
        return Ok(true);
    }
}

When I run this I get a 404 with this message:

{
  "type": ".5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-b8c375d4c3f10c5097b982cac5b01c64-be98749a0187e8fb-00",
  "errors": {
    "myContact": [
      "The myContact field is required."
    ],
    "$.contactEmails[0].contact": [
      "The JSON value could not be converted to Entities.ContactEntity. Path: $.contactEmails[0].contact | LineNumber: 8 | BytePositionInLine: 25."
    ]
  }
}

If I remove the ContactEntity property from the ContactEmailEntity, the code runs fine and I can hit the controller.

Yong Shun's recommendation was to use DTO's, or simple classes:

public class CreateContactInput
{
    [Required]  // Specify the data annotation attribute for property validation
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string CompanyName { get; set; }
    public string Title { get; set; }
    public string Suffix { get; set; }
    public string DisplayName { get; set; }
    public string Tags { get; set; }
    public string Notes { get; set; }
    public string EmailAddress { get; set; }
}

But this means having a separate class with identical properties on it, one bound to the UI with INotifyPropertyChanged and another POCO for sending & receiving data from the API. Then I would have map data back & forward.

The more I think about it this doesn't feel right. If removing the ContactEntity property from the ContactEmailEntity fixes the issue, then isn't something else wrong?

I'd like more input from you guys please.

发布评论

评论列表(0)

  1. 暂无评论