none
InvalidOperationException in Entity Framwork while adding line RRS feed

  • Question

  • I cannot add line to table because I got error: InvalidOperationException. What am I doing wrong?

    using (var database = new DatabaseEntities()) 
     {
         database.Person.Add(new Person
         {
             Name = personNameTextBox.Text,
             Surname = personSurnameTextBox.Text,
             E_mail = personEmailTextBox.Text,
             Password = Hash.HashString(personPasswordTextBox.Text),
             Phone_number = personPhoneNumberTextBox.Text
         });
         database.SaveChanges();
     }
    Thursday, December 8, 2016 8:19 AM

All replies

  • I cannot add line to table because I got error: InvalidOperationException. What am I doing wrong?

    using (var database = new DatabaseEntities()) 
     {
         database.Person.Add(new Person
         {
             Name = personNameTextBox.Text,
             Surname = personSurnameTextBox.Text,
             E_mail = personEmailTextBox.Text,
             Password = Hash.HashString(personPasswordTextBox.Text),
             Phone_number = personPhoneNumberTextBox.Text
         });
         database.SaveChanges();
     }


    Wednesday, December 7, 2016 4:02 PM
  • Hi HP coder,

    Thank you for posting in MSDN forum.

    Since Our forum is discussing about VS general question like how to set/configure Visual Studio and Visual Studio tools.

    As your question is Entity framework, I will help you move this case to Data Platform Development ADO.NET Entity Framework and LINQ to Entities forum for dedicated support

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 8, 2016 7:40 AM
  • What am I doing wrong?

    Well what you should do first is put a try/catch around the code and present a stack trace with the inner exception message that may pinpoint with more information about the exception.

    Thursday, December 8, 2016 10:18 AM
  • Hi HP coder,

    Based on your description to related code, I create a simple demo with identity primary key. it works fine. Could you please provide a bit more information, such as table structure of Person, the value of personNameTextBox.Text, personSurnameTextBox.Text, etc.

    We'll reproduce your issue on our side and try to find a solution to resolve it.

    Here is my code and related table structure.

    #Code

    string name = "Cole";
                string surname = "Wu";
                string email = "abc@123.com";
                string password = "test";
                string phoneNumber = "123123";
    
                using (var database = new EF20161102Entities1())
                {
                    database.Persons.Add(new Person
                    {
                        Name = name,
                        Surname = surname,
                        E_mail = email,
                        Password = password,
                        Phone_number = phoneNumber
                    });
                    database.SaveChanges();
                }

    #Table

    CREATE TABLE [dbo].[Persons] (
        [Id]           INT          IDENTITY (1, 1) NOT NULL,
        [Name]         VARCHAR (50) NULL,
        [Surname]      VARCHAR (50) NULL,
        [E_mail]       VARCHAR (50) NULL,
        [Password]     VARCHAR (50) NULL,
        [Phone_number] VARCHAR (50) NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC)
    );
    
    

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 9, 2016 5:47 AM
    Moderator