0

I'm trying to update the ConversationId in my users and messages table with the new ConversationId being created in my method to follow.

Once I try to save the database changes I get this error:

Cannot insert explicit value for identity column in table 'Messages' when IDENTITY_INSERT is set to OFF.

My models:

public class Conversation
{
    public int ConversationId { get; set; }
    public ICollection<User> Users { get; set; } = new Collection<User>();
    public ICollection<Message> Messages { get; set; } = new Collection<Message>();
}

public class Message
{
    [Key]
    public int MessageId { get; set; }
    public string MessageContent { get; set; }
    public DateTime DateCreated { get; set; }
}

public class NewMessageViewModel
{
    public Message Message { get; set; }
    public User Sender { get; set; }
    public User Receiver { get; set; }
}

public class User
{
    [Key]
    public int UserId { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ICollection<Message> Messages { get; set; } = new Collection<Message>();
}

And my controller method:

public async Task<ActionResult> SendMessage([FromBody] NewMessageViewModel content)
{
    var newMessage = new Message
    {
        MessageContent = content.Message.MessageContent,
        DateCreated = DateTime.Now
    };

    var newConversation = new Conversation
    {
        Users = { content.Sender, content.Receiver },
        Messages = { content.Message }
    };

    var conversations = _dbContext.Conversations;

    conversations.Add(newConversation);

    await _dbContext.SaveChangesAsync();
}

So at a glance it looks like I am not separating conversations properly? It's not getting conversations.Users or conversations.Messages. But I am unsure if this is the entire issue as I've been looking at is so much its hard to be objective.

Just looking at what I am passing through in my post request:

{
    "message": {
        "messageId": 1,
        "messageContent": "HelloWord",
        "dateCreated": "2020-05-26T11:56:43.8901969+01:00"
    },
    "sender": {
        "userId": 2,
        "username": "Test",
        "firstName": "Test",
        "lastName": "Test",
        "messages": null
    },
    "receiver": {
        "userId": 1,
        "username": "AndyStav",
        "firstName": "Andy",
        "lastName": "Stav",
        "messages": null
    }
}

So it looks like the problem is that I am passing in a value to the messageId.

I'm such a fool. I used this json earlier in some testing and didn't take it out.

Neuron
  • 5,141
  • 5
  • 38
  • 59
dros
  • 1,217
  • 2
  • 15
  • 31

1 Answers1

0
{
    "message": {
        "messageId": 1,
        "messageContent": "HelloWord",
        "dateCreated": "2020-05-26T11:56:43.8901969+01:00"
    },
    "sender": {
        "userId": 2,
        "username": "Test",
        "firstName": "Test",
        "lastName": "Test",
        "messages": null
    },
    "receiver": {
        "userId": 1,
        "username": "AndyStav",
        "firstName": "Andy",
        "lastName": "Stav",
        "messages": null
    }
}

my json was the problem!!forgot to take the id's out from where I was using this before!

dros
  • 1,217
  • 2
  • 15
  • 31