Entity structure does not store data records in database

First, I must mention that this problem only occurs in Windows form applications, and the same program in web mode, for example, works fine with MVC3.

A few days ago I wrote a very simple program in the form of windows using Visual Studio 2010 with a SQL Express database. I added a database by choosing Add> New Item> a service-based database and an entity data model based on this database. I used the Entity framework to add some new records to tables. I did such a thing with VS 2008 SP1 before that without any problems, so I did the same. The program compiled and works without errors, and I entered some new data. after exiting the program, I returned to the database and nothing happened. None of the information I entered has been saved. I debug the program step by step, and everything was in order. The code below is related to a very simple program with the indicated problem. The database has one table (book):

namespace Book
{
    public partial class BookForm : Form
    {
        BookDatabaseEntities db = new BookDatabaseEntities();

        public BookForm()
        {
            InitializeComponent();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            Book bookToCreate = new Book();

            bookToCreate.Id = Guid.NewGuid();
            bookToCreate.Title = titleTextBox.Text;

            db.Books.AddObject(bookToCreate);
            db.SaveChanges();
        }
    }
}

, - . .

.................

:

namespace Book
{
    public partial class BookForm : Form
    {
        //BookDatabaseEntities db = new BookDatabaseEntities();

        public BookForm()
        {
            InitializeComponent();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            var db = new BookDatabaseEntities();
            var bookToCreate = db.Books.CreateObject();

            //Book bookToCreate = new Book();

            bookToCreate.Id = Guid.NewGuid();
            bookToCreate.Title = titleTextBox.Text;

            db.AcceptAllChanges();
            db.Books.AddObject(bookToCreate);
            db.SaveChanges();
        }
    }
}
+5
2

, MSDN Patrice Scribe

+4

:

db.Attach(bookToCreate);
db.SaveChanges();

Edit:

( DAL), :

using (var dbContext = new DbEntities())
    {
        var job = dbContext.RiskToolJob.CreateObject();

        job.AnalysisDataID = analysisDataID;

        job.JobRmsAnalysisID = RMSAnalysisID;
        job.UserName = userName;

        job.JobCreated = DateTime.UtcNow;

        dbContext.RiskToolJob.AddObject(job);

        dbContext.SaveChanges();

        return job.DataId;
    }

, PK (DataId), , , , , .

+1

All Articles