0

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?

Javysk
  • 381
  • 2
  • 16
  • possible duplicate of [EF6 CodeFirst My \[Key\] Id Column is not auto-incrementing as an identity column should](http://stackoverflow.com/questions/23147479/ef6-codefirst-my-key-id-column-is-not-auto-incrementing-as-an-identity-column) – jamesSampica Aug 04 '15 at 13:06
  • Maybe, I didn't find an answer until now and I found my answer here http://stackoverflow.com/questions/14612813/entity-framework-code-first-using-one-column-as-primary-key-and-another-as-auto I thought this problem was for EF version – Javysk Aug 04 '15 at 13:21

2 Answers2

0

Try adding the attribute [Key] above your ID This should set it as the primary key for the table that's generated by EF.

    [Key]
    [Column("ID")]
    public decimal Id { get; set; }  
John Grabanski
  • 363
  • 7
  • 20
  • Thanks John Id had Key attribute, only I need put this other attribute [DatabaseGenerated(DatabaseGeneratedOption.Identity)] and works ok. – Javysk Aug 04 '15 at 12:55
0

I need put this other attribute [DatabaseGenerated(DatabaseGeneratedOption.Identity)] and works ok.

Answer in this post: Entity Framework Code First Using One column as Primary Key and another as Auto Increment Column

Finally:

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("ID")]
        public decimal Id { get; set; }
Community
  • 1
  • 1
Javysk
  • 381
  • 2
  • 16