简体   繁体   中英

How do I get a SQL Server Express database and DbContext object to share data?

I am still pretty new at all of this so hopefully this is a quick fix.

I have a SQL Server Express database created via Microsoft SQL Server Express. I am using Visual Studio Community edition.

I am trying to create a simple program to input/retrieve/manipulate database information.

If I set my database as the programs datasource and then bind form controllers to the database table, the form controls display the records in the tables from the database. No problem there, but I want to be able to use the data easily inside the program, so I am looking into DbContext .

I can generate the DbContext via Visual Studio's 'create model from database' wizard just fine and its table linking looks correct.

If I declare the DbContext and then set it as a datasource for the project, this works and records are generated as I create them. I can use the objects to manipulate data as I wanted to.

The records are however not shared with the database. Also the data from the database is not sent to the program this way. eg.existing records created inside SQL Server Express are not included on startup as they would be if I link directly to the database.

Do you know how I can actually use the DbContext to both receive existing database information, and to send new/updated data back to the SQL Server database?

There must be a simple answer, and it is likely already here. I just find it a bit hard to find the specific answer I am looking for when I am still so new that I don't know how to ask the question clearly.

Why wont they share data? are there some commands I am overlooking?

Cheers

If you created your database in SQL Server Management Studio inside your Express edition database, then most likely it's stored in the SQLEXPRESS instance of SQL Server which you can access with server/instance name of .\\SQLEXPRESS or (local)\\SQLEXPRESS or YourPcName\\SQLEXRPESS .

Visual Studio however tends to use the LocalDB instance of SQL Server Express - the developer oriented "version" of SQL Server - so your connection string might use something like (localdb)\\v11.0 or something like that - which basically connects to a whole different instance of SQL Server.

If you want to use the SQLEXPRESS instance of your database - the one you've created with the SQL Server Management tools - then just make sure your Entity Framework connection string (typically in your web.config file) also uses (local)\\SQLEXPRESS as the data source= (or server= ) parameter in the connection string

Some good suggestions here that will help someone. As it stands I didn't realize that I had to manually load and save data from the SQL Database.

I needed to create a DbSet and set it to equal the entity table. eg

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.Entity;
namespace Gui_mockups
{
    public partial class Form1 : Form
{
    Lab_Assistant_backendEntities Database;
    DbSet data;



    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Database = new Lab_Assistant_backendEntities();
        data = Database.Sample_Register;
        data.Load();
        sample_RegisterBindingSource.DataSource = data.Local;

    }

    private void sample_RegisterBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        Database.SaveChanges();
    }
}

So, I needed to bounce the table to a DbSet and then set the DbSet to be the BindingSource.DataSource

this will allow you access to the SQL data.

to save new records use the .SaveChanges() method for the entity framework object.

My advice to other noobs is to understand that while your entity framework is linked to your SQL Table, this doesn't mean the data is automatically linked. you still need to load and save the information yourself in order to update the Database(and the object).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM