`

public void DataBaze() { using (Test_Baza_ASP_Entities baza = new Test_Baza_ASP_Entities()) { Test_Table dataContact = new Test_Table() { Index_name = name, }; baza.Test_Tables.Add(dataContact); baza.SaveChanges(); } }` 

enter image description here enter image description here enter image description here

ASP.NET and just C # users and database specialists) Help me find the problem. I perform a simple learning task in ASP.NET. The goal is to read data from textboxes and write to the database using DetailsView. Everything is simple, especially when Entity has already passed and there is an application and a working base (Avrbaza) with a table where I am recording data using the method of creating an EF model.

At first, I decided to simply code in the old fashion through the creation of a model without DetailsView to write data. For my task I create a new database (test_asp_baza) as I created the old one, I create the table as I created the old one. I create connection as well. The code is clear, simple and beautiful as in my working application. The result is an error on the moment. SaveChanges ()

We go further. I do according to the DetailsView method and right in the constructor I create a connection where I have the necessary items in the table (INSERT, UPDATE DELETE) I cannot select

Further more interesting. I take the old working base through “I don’t want” (it’s working and I don’t want to touch it) and create a table there. The result is the same - does not want to record and give access.

And then the most important thing. I decided to take a chance and write test data to the work table from the work base (Avrbaza). And then everything works on both methods. DetailsView the necessary items, you can select the EF, too, through the model without any problems writes down.

Question: How did I create that table so that I can change everything there, but not in a new one, even if it is created in a working database? Checked all the properties of the table - everything is identical. DBMS Microsoft SQL Server. Base I create in SQL Server 2014 Management Studio

  • one
    Insert all the necessary screenshots and problem code here, links somewhere there hardly anyone will look. - tym32167
  • Corrected. Thank you - FX_Sektor
  • Apparently, you have Database First. How do you switch between databases? just replacing the connection string in the table? or updating the model in the designer? If the second is, then, apparently, you don’t have PrimaryKey in the problem table in the “new” database, and EF simply cannot generate sql for insertion (all about the main key fields should be selected) - because without primary key is unclear exactly how to distinguish records from each other - PashaPash ♦
  • You have a problem in the code, right? How do you expect to help you with the code if the code in question is not? Your verbal code descriptions do not make sense, since if you knew exactly what you were doing, then there would be no questions. So you do not understand something somewhere, and therefore your explanation is a rough description of the incorrect interpretation of the problem code .. Because the code in the studio is tym32167
  • PashaPash No, with PrimaryKey it is announced there and there when creating the table in the same way (it generates automatically). I have already come across this and know exactly how to deal with it, that there is no point in the keys - FX_Sektor

1 answer 1

Here's how it was decided: I went through VS to the designer of the working and non-working table and saw the difference)) it’s generally strange, it generates this thing automatically, if initially in the SQL MS itself I specify Identity Specification (is Idetity) 1.1. But when I created the connection and went through the designer to the table and saw that there is no key automatically and there is not enough row at the bottom as in the work table "CONSTRAINT [AK_Table_Test_Column] PRIMARY KEY CLUSTERED ([key] ASC)" and everything plows)) thank you .. .. PS is still strange, it should automatically indicate this.