LINQ到SQL -MDF DB中的DEBUG文件夹中的MDF DB在插入上更改,而不是附加的副本



我在LINQ中有一个插入工作(一种),但它编辑了解决方案的调试子文件夹中的SQL数据库的副本。我将此特定的数据库作为基于服务器的数据库制作,然后制作了一个linq-to-sql类以编辑它。该插入确实有效,因为它插入了输入的任何数据,即将插入DB的错误副本中。我根本看不到VS中的插入物或获取该数据。

我不是linq to-sql的专家,所以也许我做了一些可怕的错误?

c#插入:

        public TournDataContext db = new TournDataContext(); //Using Linq to Sql to connect to the DB.
        private void BtnNewPlayerSubmit_OnClick(object sender, RoutedEventArgs e) // Method called when the user clicks the register button.
    {
        string fName;
        string surname;
        string email;
        string password;
        DateTime dob;
        if (tbxFirstName.Text != null && tbxSurname.Text != null && tbxEmail.Text != null &&
            tbxPassword.Text != null && DateTime.TryParse(tbxDOB.Text, out dob)) // Checking for nulls.
        {
            fName = tbxFirstName.Text;
            surname = tbxSurname.Text;
            email = tbxEmail.Text;
            password = tbxPassword.Text;
            dob = DateTime.Parse(tbxDOB.Text);
            int newID = db.Players.Count() + 1;
            Player aPlayer = new Player
            {
                DOB = dob,
                Email = email,
                FirstName = fName,
                Paid_Fees = 'N',
                Password = password,
                Surname = surname,
            };
            db.Players.InsertOnSubmit(aPlayer);
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {    
                Console.WriteLine(ex);
            }
        }

SQL表定义,值得:

CREATE TABLE [dbo].[Players] (
[Id]        INT           IDENTITY (1, 1) NOT NULL,
[FirstName] NVARCHAR (30) NOT NULL,
[Surname]   NVARCHAR (30) NOT NULL,
[Email]     NVARCHAR (50) NOT NULL,
[DOB]       DATE          NOT NULL,
[Password]  NCHAR (30)    NOT NULL,
[Paid Fees] NCHAR (1)     DEFAULT ('N') NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)

);

连接字符串:

    <connectionStrings>
    <add name="RegisterPlayer.Properties.Settings.TournamentDBConnectionString"
        connectionString="Data Source=(LocalDB)v11.0;AttachDbFilename=|DataDirectory|TournamentDB.mdf;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

显然,当您首次创建解决方案/项目数据库时,它是在项目的父级目录中创建的。之后,当您构建/调试解决方案时,它将在垃圾箱/调试目录中添加数据库。调试时,您对数据进行的任何更改都在调试数据库中进行。但是,当您每次在vs中运行应用程序时,它会从父级目录中提取数据,该数据从未收到您在调试时进行的更改,因为更改实际上是在调试数据库中进行的。

解决方案:

•In database explorer
•Right click on the database
•Select modify connection
•Advanced options
•Search for the property AttachDbFileName
•Change it to the DB from debug folder c:...bindebugDB.mdf
•Once you point your database to the Debug directory, in VS Server Explorer, you can then delete the database in the solution explorer and all updates will transpire in the Debug database.

最新更新