mardi 4 août 2015

Entity Framework 4.1 dont generate Id when save data

I'm trying to do something simple, but this dont work in EF 4.1,it does in EF 6, but I cant update version because I need this application in an old server and doesnt support it.

This is my code:

               chat = new Chat()
                        {
                            AdminId = 99
                        };

                        db.Chat.Add(chat);
                        db.SaveChanges();

                var cp = new List<ChatPeople>();
                        foreach (int user in usersids)
                        {
                            cp.Add(new ChatPeople
                            {
                                ChatId = chat.Id,
                                UserId = user
                            });
                        }
                        cp.ForEach(c => db.ChatPeople.Add(c));

and this is the model:

[Table("CHATS", Schema = "SCHEMA")]
    public class Chat
    {     

        [Column("ID")]
        public decimal Id { get; set; }       

        [Column("ADMINID")]
        public decimal AdminId { get; set; }

        public virtual ICollection<ChatPeople> ChatPeople { get; set; }

        public virtual ICollection<ChatHistory> ChatHistory { get; set; }
    }

(I need decimal Id because int dont work in Oracle.)

My problem is when I save Chat, this dont return the current Id of this registry in database and when in ChatPeople try to do this ChatId = chat.Id, I dont have the value. In DB Chat are saving ok with AdminId = 99 and Id = (Autonumeric-Identity)

What can I do to get Id of registry that are being saved?

Aucun commentaire:

Enregistrer un commentaire